I have searched for examples to understand the for each
loop and most examples only show using the type String
for example declare a array
type String
and then use the loop for each
.
Well, the point is that I am watching a course in a video tutorial and I have been stuck because I do not have a clear idea of understanding this, here I leave the code and my doubts:
package PildorasInformaticas;
// uso de bucle for each para recorrer un bucle bidimensional
public class V26_Arrays_Bidimensional2 {
public static void main(String[] args) {
// declarar una matriz de dos dimensiones e incializa sus valores
int[][] matrix = {
{10,15,18,19,21},
{5,25,37,41,15},
{7,19,32,14,90},
{85,2,7,40,27}
};
for (int[] fila:matrix){
System.out.println("");
for (int z : fila){
System.out.printf("%02d ",z);
}
}
}
}
It is a array
two-dimensional of 4 * 5 elements, my doubts are when it begins to travel with the loop for each
, why declare a array
within the for(int[])
and why not only use the type of data int
without the need of ([]) as when you scroll a array
of type String
, I tried to delete the ([]) and I get an error that says:
incompatible types: int [] can not be converted to int. for-each not applicable can not be to expression type.