In java the integer data types are applicable in the for each loop?

1

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.

    
asked by Vladimir Joel 05.09.2017 в 08:51
source

3 answers

1

To understand what happens, you have to see what object you are acting on. In your case, you declare a matrix formed by rows and columns. With this foreach you go through the rows:

for(int[] fila : matrix){...}

And as you have done well, to access the integers, you have to go through the row with

for(int z : fila){...}

Why does this happen? It's simple, you have to go step by step accessing the "objects", in this case arrays.

An example is, if you have an array of people and want to print the names of their children, you can not do it directly, you have to go through people and then your children to get the name, it's the same.

If you had an array of integers simply (or a list can also be traversed with a for each) with the second loop it would be valid.

When you declare it like this:

for(int z : matrix){...}

It gives error because matrix is not formed by integers, it is formed by arrays of integers, therefore, first you have to go through the array and then collect the integers.

I hope you have understood it a little better.

    
answered by 05.09.2017 / 09:22
source
1

Good, First of all we have a declared matrix:

int[][] matrix = {
                {10,15,18,19,21},
                {5,25,37,41,15},
                {7,19,32,14,90},
                {85,2,7,40,27}
        };

This means that the array is composed of other arrays . To enter the matrix, the first loop is used:

for (int[] fila:matrix){}

Where the rows of the array matrix are represented one by one whose content is another array , so an array has to be recovered de ints: Example:

  

"If I have a sack that contains bags of goodies, to get a treat from the sack, first I have to take out a bag that contains those   goodies "

I know that it is not the best example, but it is to understand it better. What is being done is to retrieve an array of integers within that array that contains integer arrays. So, inside the first loop we already have the arrays that contain the integers and we start to go through each one of those arrays in the second loop (inside the previous loop).

for (int z : fila){}

In this loop, each row contains an integer

int[][] matrix = {
                {10,15,18,19,21}, //<-- Esto es una fila
                {5,25,37,41,15}, //<-- Esto es una fila
                {7,19,32,14,90}, //<-- Esto es una fila
                {85,2,7,40,27} //<-- Esto es una fila
        };

Then in the loop we'll be inside the "bag of goodies" to retrieve each of those integers

    
answered by 05.09.2017 в 09:29
1

In the first for you are adding to each array called fila each line of the multidimensional array. In total it will put 4.

In the second for is traveling through each of the rows of the array (it has 4 rows and 5 values in each) fila and is assigning them in z . So, then make a print of z .

All this can be done in the way you were looking for in the following way:

      public class JavaFiddle
  {
    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){
        for (int i=0; i<matrix.length; i++){
            System.out.println("");

            //for (int z : fila){
            for (int z=0; z<matrix[i].length; z++){
                System.out.printf("%02d ",matrix[i][z]);
            }
        }

    }
  }
    
answered by 05.09.2017 в 09:16