-
侃侃无极
你需要的是命名数组。我想写下面的代码:int[] n = new int[4];for(int i=1;i<4;i++){
n[i] = 5;}
-
ITMISS
你应该使用List或array代替List<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);要么int[] arr = new int[10];arr[0]=1;arr[1]=2;甚至更好Map<String, Integer> map = new HashMap<String, Integer>();map.put("n1", 1);map.put("n2", 2);//conditionally get map.get("n1");
-
慕桂英546537
Java中的动态变量名称没有这样的东西。在您的情况下,您可以使用数组:int[] n = new int[3];for() {
n[i] = 5;}对于更一般的(name, value)配对,请使用Map<>
-
墨色风雨
试试这种方式: HashMap<String, Integer> hashMap = new HashMap();
for (int i=1; i<=3; i++) {
hashMap.put("n" + i, 5);
}
-
翻翻过去那场雪
你没有。您可以做的最接近的事情是使用Maps来模拟它,或定义您自己的对象来处理。