0

Supposedly i want to loop through the 2d array vertically for example given a 2*2 array

  • a[0] a[0]
    a[1] a[0]
    a[2] a[0]

How do i output the values accordingly to the bold part

2 Answers 2

3
for (int i = 0; i < a.length; i++) {
    System.out.print(a[i][0]);
}

Of course, you can replace the 0 with any in-bounds column.

Sign up to request clarification or add additional context in comments.

Comments

1

Since you want to loop through the array vertically by changing the first dimension, you need to have the variable in the inner for loop to be the first dimension like the code below:

for(int i=0; i<3; i++){
             for(int j=0; j<3; j++){
                 System.out.println(a[j][i]);
             }
         }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.