Use an array. In Javascript, place var results = [] before your loop and append results using results.push(value). In Java, you'll want to use an ArrayList. (Those are very different languages, by the way.)
List<Integer> results = ArrayList<Integer>();
while (...) {
// Do computation ...
int result = ...
results.add(result);
}
// Now we have all of the results in 'results'
或者,如果您想将每个结果绑定到不同的名称,您可以这样做像这样的东西:
Map<String, Integer> results = HashMap<String, Integer>();
while (...) {
// Do computation ...
String name = ...
int result = ...
results.put(name, result);
}
Hopefully this makes sense.
Unfortunately, it doesn't.
In Java it makes no sense to create variables on the fly. It is extremely difficult to do, and once you have done it they are extremely difficult to use. (By contrast, it is easy to do in Javascript ...)
However, this just means that you need to do what you are trying to in a different way. For instance, the following does a computation in a loop and then saves the results in an (existing) ArrayList variable:
List<Integer> results = ArrayList<Integer>();
while (...) {
// Do computation ...
int result = ...
results.add(result);
}
// Now we have all of the results in 'results'
Or, if you want to bind each of the results to a distinct name, you could do something like this:
Map<String, Integer> results = HashMap<String, Integer>();
while (...) {
// Do computation ...
String name = ...
int result = ...
results.put(name, result);
}
地图<字符串,对象> declareVariableRuntime= new HashMap(); declareVariableRuntime.put("variableName", new Object());
No, It is not possible to declare variables in java at runtime. But java provides java.util.map, which can be used like in the example below. We can assume that the key is the variable name.
Map<String, Object> declareVariableRuntime= new HashMap<String, Object>(); declareVariableRuntime.put("variableName", new Object());
发布评论
评论(4)
使用数组。在 Javascript 中,将
var results = []
放在循环之前,并使用results.push(value)
附加结果。在 Java 中,您需要使用ArrayList
。 (顺便说一下,这些是非常不同的语言。)Use an array. In Javascript, place
var results = []
before your loop and append results usingresults.push(value)
. In Java, you'll want to use anArrayList
. (Those are very different languages, by the way.)不幸的是,事实并非如此。
在 Java 中,动态创建变量是没有意义的。这是非常难做到的,一旦你做到了,它们就很难使用。 (相比之下,在 Javascript 中很容易做到......)
但是,这只是意味着您需要以不同的方式做您想做的事情。例如,以下代码在循环中进行计算,然后将结果保存在(现有的)ArrayList 变量中:
或者,如果您想将每个结果绑定到不同的名称,您可以这样做像这样的东西:
Unfortunately, it doesn't.
In Java it makes no sense to create variables on the fly. It is extremely difficult to do, and once you have done it they are extremely difficult to use. (By contrast, it is easy to do in Javascript ...)
However, this just means that you need to do what you are trying to in a different way. For instance, the following does a computation in a loop and then saves the results in an (existing)
ArrayList
variable:Or, if you want to bind each of the results to a distinct name, you could do something like this:
以下是我实施并帮助我轻松修复解决方案的方法,没有太多障碍。
// 创建数组列表
迭代循环并将对象添加到带有索引的数组列表中。
//在运行时借助索引检索对象
Following is the way that i have implemented and helped me to fix my solution easily without much hurdles.
// Creating the array List
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
不,Java 中不可能在运行时声明变量。但是java提供了java.util.map,可以像下面的例子一样使用它。我们可以假设键是变量名。
地图<字符串,对象> declareVariableRuntime= new HashMap(); declareVariableRuntime.put("variableName", new Object());
No, It is not possible to declare variables in java at runtime. But java provides java.util.map, which can be used like in the example below. We can assume that the key is the variable name.
Map<String, Object> declareVariableRuntime= new HashMap<String, Object>(); declareVariableRuntime.put("variableName", new Object());