Multidimensional Array or Matrix: Array of arrays


You saw in the last Java tutorial of our course that an array is a set of variables of the same type.

However, we could also declare a set of arrays. In this case, we would have a multidimensional array or multidimensional vector, also known as matrices.



Multidimensional Arrays or Vectors: Set of vectors or arrays in Java

Imagine the situation that came round, using as example in our course online Java Progressive, the exemplo data of students of a school.
Now a student makes several tests every year.

Suppose he makes 10 tests a year. Well, you are already a Java programmer, will not declare 10 floats to store 10 notes. Will declare an array of 10 elements floats.

However, there are several matters in college: Mathematics, Physics, Chemistry, Biology etc.
Suppose there are 10.
And there's going to declare 10 vectors / arrays?
Of course not, that would be very tiring.
Declare an array of arrays.

Arrays of one or more dimensions in Java

In past examples, we just declared a block of variables. We say that this vector is one-dimensional, because it is only one array.
See this array / vector only as a line, so you will understand the notion of dimension.
For example, let's declare a one-dimensional array with 5 grade of Mathematics:
(let's suppose the grades are between 0 and 10)
int[] grades = {8.0, 7.5, 8.5, 9.0, 8.0};

The note can be represented by a 1x5 matrix, ie a row and five columns:
8.07.58.59.08.0


Now we represent the notes in Physics, Mathematics below. We will have a 2x5 array, or an array of five columnss and two lines:
8.07.58.59.08.0
8.99.08.68.48.0


Now we represent the notes of Chemistry, below the Physics notes.
We will have a 3x5 matrix, or a matrix of three lines and five columns:
8.07.58.59.08.0
8.99.08.68.48.0
6.87.17.07.66.5


Matrix:  representing multidimensional arrays in Java

The syntax is exactly the same as the normal array, the difference is in the number of brackets '[]' to use.
In this case, we use a pair for each dimension.

For example, to declare the 2x5 matrix of the example above:
float[][] grades = new float [2] [5];

Or
float[][] grades = {{8.0, 7.5, 8.5, 9.0, 8.0}, {8.9, 9.0, 8.6, 8.4, 8.0}};


To declare a 3x5 matrix from the previous example:
float[][] grades = new float [3] [5];

Or
float[][] grades = {{8.0, 7.5, 8.5, 9.0, 8.0}, {8.9, 9.0, 8.6, 8.4, 8.0}, {6.8, 7.1, 7.0, 7.6, 6.5}};

Note that grades[0] refers to the array of notes of Mathematics.
Note that grades[1] refers to the array of notes in Physics.
Note that grades[2] refers to the grades array of Chemistry.

For example: what was the fourth note of Physics student?
You now the array of Physics grades is grades[1], and the fourth note is the element [3] of this array.
Then, the fourth grade physics student is stored in: grades[1] [3], which is 8.4


Examples of codes about multidimensional Array


As usual, in our of Progressive Java course, we will present two examples of how to use dimensional arrays, or Matrix, in Java.

Example 1: Create a Java application that asks the user to fill a 3x3 matrix with integer values ​​and then display this array.

The great new, and important thing in this type of application are nested loops, ie, one control statement inside the other.

First we create a loop that will go through all the lines of the matrix. We can, and we must see each line as an array of 3 elements.
Within each line, we have to go through each element of the array and provide a value. We do this through another loop, which will be responsible for 'columns' forming our nested loops.

To print, the layout is exactly the same. Print line by line, and in each line, print column by column.

Our Java code is:


import java.util.Scanner;

public class matrixTest {
    
    public static void main(String[] args){
            int[][] matrix = new int[3][3];
            
            Scanner input = new Scanner(System.in);
            System.out.println("Matrix M[3[3]\n");
            
            for(int line=0 ; line < 3 ; line++){
                for(int column = 0; column < 3 ; column ++){
                    System.out.printf("Insira o elemento M[%d][%d]: ",line+1,column+1);
                    matrix[line][column]=input.nextInt();
                }
            }
            
            System.out.println("\nA The matrix now is: \n");
            for(int line=0 ; line < 3 ; line++){
                for(int columna = 0; column < 3 ; column ++){
                    System.out.printf("\t %d \t",matrix[line][column);
                }
                System.out.println();
            }
           
        }


}


Example 2: Create a Java application that asks the user to fill a 3x2 matrix with integer values ​​and then display this array.

In the last example, the number of lines was equal to the number of columns of the matrix. Let's use a different example, for you to fix your knowledge in multidimensional arrays.

As stated earlier, an multidimensional array, matrix or multidimensional vector is nothing more than a set of set of vectors or arrays, i.e, is an array of arrays.

When we do: int[5] to declare an array of integers, we declare 5 variables of type integer.
When we do: int[10][5], we are declaring 10 arrays, and in wich one has 5 integers. Or 10 arrays of the type of the past example. Therefore, the size of this array - length - is 10.
You can obtain this value by command: array.length

Thus, a 3x2 matrix size is 3.
A 4x3 has size 4, a size of the array 10x123123 is 10, and so on.
That is, the length of multi-dimensional arrays is the number of lines.

Let's see the how Java code will be:


import java.util.Scanner;

public class matrixTest {
    
    public static void main(String[] args){
        int[][] matrix = new int[3][2];
        
        Scanner input = new Scanner(System.in);
        System.out.println("Matrix M[3][2]\n");
        
        for(int line=0 ; line < 3 ; line++){
            for(int column = 0; column < 2 ; column++){
                System.out.printf("Type the element M[%d][%d]: ",line+1,column+1);
                matrix[line][column]=input.nextInt();
            }
        }
        
        System.out.println("\nA The Matrix now is: \n");
        for(int line=0 ; line < 3 ; lline++){
            for(int column = 0; column < 2 ; column++){
                System.out.printf("\t %d \t",matrix[line][column]);
            }
            System.out.println();
        }
       
    }


}





No comments: