是否可以在Java中动态生成变量名称? [英] Is there away to generate Variables' names dynamically in Java?
问题描述
假设我需要生成变量以保存用户的一些输入(我不知道它们有多少)。不使用 Array
, ArrayList
(以及其他类型的列表和地图)我的代码可以生成(假设) String
变量X次,名称如( String var001
, String var002
,字符串var003
等)?如果是,请提供示例代码。
Let's say that I need to generate variables to hold some input from the user (I don't know how many they are). Without using Array
, ArrayList
(and other kind of lists and maps) can my code generate (lets say) String
variables X times with names like (String var001
, String var002
, String var003
, etc)? If yes, please provide sample code.
推荐答案
以下是我实施并帮助我轻松修复解决方案的方式很多障碍。
Following is the way that i have implemented and helped me to fix my solution easily without much hurdles.
//创建数组列表
List accountList = new ArrayList();
for(int k=0;k < counter;k++){
accountList.add(k, (String)flowCtx.getValueAt("transitId"+m));
}
迭代循环并将对象添加到带索引的arraylist中。
Iterating the loop and adding the objects into the arraylist with the index.
//在索引的帮助下在运行时检索对象
//Retrieving the object at run time with the help of the index
String a = accountList.get(i));
这篇关于是否可以在Java中动态生成变量名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!