How to use for each in array of arrays(array)?

Asked

Viewed 3,080 times

1

Having the array of arrays:

 int matriz[][] = new int[1][1]

How do I use for each (for(int Count : array)) to traverse your rows and columns?

1 answer

4

The namer to traverse the array using "foreach" is as follows:

int matriz[][] = new int[1][1];

int valorRecebido = 0;

for (int[] vetor : matriz) {
    for (int elemento : vetor) {
        valorRecebido = elemento;
    }
}

Note that in this way, all rows and columns of the matrix are traversed and accessed by the variable elemento.

Note that the outermost loop returns a vector of a dimention per iteration and the innermost loop returns an element, again by iteration. In this way, the whole vector is traversed.

  • Thanks for the feedback, but I was wondering how to do this using "for each", that "for(int i : array)".

  • Oops! Give me a few minutes

  • All right, thank you.

  • Please, if the answer is correct, mark the green "check" that appears below the response counter. Thank you :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.