Java Multidimensional Arrays

Multidimensional arrays

int [][] myIntArray = {{7,3,6},{3,1,2},{9,9,3}}; // Create a new md array with values System.out.println( myIntArray[0][2] ); // Access the first array, third item myIntArray[1][0] = 5 ; // Modify the second array, first item
  • A multidimensional array is just an array of arrays.
  • To access the value in an MD array, specify the array name followed by the index indicating which array, then put the index specifying which item in that array.

Creating empty MD arrays

int [][] myIntArray = new int [7][]; // Creates an array of 7 arrays
  • You only have to specify the length of the first array.

Getting length of MD arrays

int muNumArrays = myIntArray.length; int myLengthOfFirst = myIntArray[0].length;

Challenge

Create a 3x3 array with 1-3 in the top row, 4-6 in the middle row, and 7-9 in the 3rd row. Modify the value at the first column and second row to be 7. Modify the value at the second column and third row to get the value in the last column and the last row. Now create an empty array that will have as many rows as the other one, even if its size gets modified later.

Quiz

Completed