Multidimensional Arrays Flashcards
Give all the ways a multidimensional array can be declared and initialized !
int[][] matice_1 = new int[][]{}
int[][] matrice_2= {{},{}}
int[][] matrice_3 =new int[3][2];
int[][] tab1;//Declaring a 2D array
int tab2[][];//Declaring a 2D array
int[] tab3[];//Declaring a 2D array
int[] tab4[][]; //Declaring a 3D array
int[] tab5[],tab6[][];//Declaring a 2D and a 3D array
Explain what happens in memory when you initialize an array of multiple dimensions !
EXAMPLE 1 :
String[][] matrice = new String[3][2];
matrice is a reference variable that points to a two-dimensional array of String objects
The array has 3 elements (rows), and each element in this outer array is itself a reference to a one-dimensional array.
Each of the inner arrays (rows) has 2 elements (columns), and these elements are String references.
Default Values: Initially, all the String references in this 3x2 array are set to null because String is an object type, and object references default to null when not explicitly initialized.
EXAMPLE 2 :
int[][] matrice = new int [3][4];
int[][] matrice: This declares a variable named matrice that will hold a reference to a 2D array of int values.
new int[3][4]: This creates a new 2D array with 3 rows and 4 columns, where each element is of type int
matrice: This variable is a reference to the entire 2D array.
The array is composed of 3 rows (each row is a 1D array of int).
Each row contains 4 int values.
Overall, matrice references a 2D array with dimensions 3x4.
Detailed Memory Representation:
matrice itself is stored on the stack (if declared locally within a method).
The actual 2D array and its inner arrays are allocated on the heap
matrice[0]: A reference to the first 1D array in the 2D array.
matrice[1]: A reference to the second 1D array, and so on.
matrice[0][0]: The first int in the first 1D array. It is a primitive int with a default value of 0.
Give all the ways an asymmetric array can be initialized !
An asymmetric array (more commonly known as a jagged array in Java) is a 2D array where the sub-arrays (or rows) can have different lengths. This means that the array does not form a perfect rectangular grid.
Declaration and Initialization with Specific Row Sizes
int[][] jaggedArray = new int[3][]; // Declare a 2D array with 3 rows
// Initialize each row with different lengths
jaggedArray[0] = new int[2]; // First row with 2 columns
jaggedArray[1] = new int[4]; // Second row with 4 columns
jaggedArray[2] = new int[3]; // Third row with 3 columns
Inline Initialization with Values
int[][] jaggedArray = {
{1, 2}, // First row with 2 elements
{3, 4, 5}, // Second row with 3 elements
{6, 7, 8, 9} // Third row with 4 elements
};
Combining Declaration and Inline Initialization
int[][] jaggedArray = new int[][] {
new int[] {1, 2}, // First row with 2 elements
new int[] {3, 4, 5}, // Second row with 3 elements
new int[] {6, 7, 8, 9} // Third row with 4 elements
};
Using Loops for Initialization
int numRows = 3;
int[][] jaggedArray = new int[numRows][];
// Initialize each row with varying lengths
for (int i = 0; i < numRows; i++) {
jaggedArray[i] = new int[i + 2]; // For example, length increases with the row index
}
Explain what happens in memory when you initialize an asymmetric
array !
int[][] jaggedArray = new int[3][];
First, Java allocates memory for the outer array, which is an array of references to other arrays (the rows). jaggedArray: A reference variable that points to an array capable of holding 3 references to 1D arrays (rows).
Allocation of Each Row
After declaring the outer array, you initialize each row (which is itself an array). Each row can have a different length.
jaggedArray[0]: Points to a new 1D array with 2 elements (default values are 0).
jaggedArray[1]: Points to a new 1D array with 4 elements (default values are 0).
jaggedArray[2]: Points to a new 1D array with 3 elements (default values are 0)
Give how we can print elements of multidimesional arrays using the
2 types of loops !
int[][] matrice= new int[2][3];
for (int i=0;i<matrice.length ;i++ ) {
for (int j=0; j<matrice[0].length;j++ ) {
System.out.print(matrice[i][j]+
”
“
);
}
System.out.println();
}
/*PRINTS
0 0 0
0 0 0 */ System.out.println(); System.out.println();
for (int[] elt : matrice ) {
for (int i=0;i<elt.length ;i++ )
System.out.print(elt[i]+
”
“
);
System.out.println();
}
/*PRINTS
0 0 0
0 0 0 */