好吧,我不完全确定我的标题措辞是否正确,但我想使用一个 int 变量来定义另一个 int 来执行方法。换句话说,我希望能够让 int/name/thingy 变量通过++ 语句,然后下一个变量将通过该方法。这可能吗?
最佳答案
int 数组可能会解决您的问题。该数组存储您的整数(“变量”),另一个数组通过索引运行(您的++ 操作):
int[] values = loadValuesInArray(); // some magic to get the populated array
for (int i = 0; i < values.length; i++) {
myMagicMethod(values[i]); // calling the method with int values
}
<小时/>
如果您需要命名变量,那么您可以使用映射:
Map<String, Integer> variables = new HashMap<String, Integer>();
variables.put("a", 1);
variables.put("b", -10);
variables.put("c", 25);
myMagicMethod(variables.get("b")); // calls method with value from "variable" b
关于java - 在 Java 中使用变量作为其他变量的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9567753/