Why when you print an array (array. [variable] .length) does it return the columns?

0
   public static void main(String[] args) {
    int[][] tabla=new int [5][3];

    System.out.println(tabla.length);

    for (int filas = 0; filas < tabla.length; filas++) {
        for (int columnas = 0; columnas < tabla.length; columnas++) {
            System.out.println(tabla[filas].length+" filas");
            System.out.println(" ");
            System.out.println(tabla[columnas].length+" columnas");
        }
    }




}

I know that when printing table.length gives me the number of rows, but I do not understand why when printing the variable rows (table [rows] .length) and the one of the columns (table [columns] .length) both show the number of columns. I have read on the internet that they say that they start counting in the line as if it were zero, but I do not finish it.

    
asked by David Palanco 29.12.2017 в 20:31
source

1 answer

0

Of course, when you do:

System.out.println(tabla.length);

This returns the size of the first dimension of the array, or what you refer to as the number of rows . According to the declaration of your array, this is being 5 .

Now, if you do:

System.out.println(tabla[x].length);

... it does not really matter what the value of x is, this will return the size of the second dimension of your array, or what you refer to as the amount of columns . According to the declaration of your array, this is being 3 .

So it does not matter if the variable is called filas or columnas , the name of the variable is irrelevant. What matters is that when using the variable between the first brackets:

// Es todo lo mismo
System.out.println(tabla[filas].length);
System.out.println(tabla[columnas].length);
System.out.println(tabla[0].length);
System.out.println(tabla[1].length);
System.out.println(tabla[2].length);
System.out.println(tabla[3].length);
System.out.println(tabla[4].length);

... you are actually asking for the same data: the size of the second dimension of the array, which is 3 .

    
answered by 29.12.2017 / 20:58
source