简体   繁体   English

数组列表 <Integer> 使用set方法时输出错误的结果

[英]ArrayList<Integer> is outputting the wrong result when using the set method

ArrayList<Integer> num = new ArrayList<Integer>();
num.add(0);
num.add(0);
num.add(0);
System.out.println(num.set(1, 2));//I don't know why it's outputting 2's down the
                                  //second column
System.out.println("   0  1  2");
int counter1 = 0;
for(int row : num) {
    System.out.println(counter1 + " " + num);
    counter1 += 1;
}

I need help figuring out why the 3 by 3 array is outputting 2's down one column. 我需要帮助弄清楚为什么3 x 3数组在向下一列输出2。

On each iteration you are printing the entire ArrayList ( num ), instead of just the relevant element ( row ): 在每次迭代中,您将打印整个ArrayListnum ),而不仅仅是相关的元素( row ):

for (int row : num) {
    System.out.println(counter1 + " " + row);
    // Here ----------------------------^
    counter1 += 1;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM