How to use for each in arrays array?

1

Having the arrays array:

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

How do I use the for each (for (int count: array)) to traverse its rows and columns?

    
asked by anonymous 20.05.2015 / 05:19

1 answer

3

The name of traversing 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;
    }
}

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

Note that the outermost loop returns a vector of one dimension per iteration, and the innermost loop returns one element, again by iteration. In this way, every vector is traversed.

    
20.05.2015 / 05:27