AetherRemnant
AetherRemnant

Reputation: 111

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.

Upvotes: 0

Views: 55

Answers (1)

Mureinik
Mureinik

Reputation: 311403

On each iteration you are printing the entire ArrayList (num), instead of just the relevant element (row):

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

Upvotes: 1

Related Questions