8) Arrays & ArrayLists Lesson

What is a Multi-Dimensional Array?

9 min to complete · By Ryan Desmond

In Java, you can nest Arrays. You learned about nesting during your introduction to if statements, but as a reminder, this means that you can put Arrays inside of Arrays.

How to Use a Multi-dimensional Array in Java?

A multi-dimensional array means that each index in the array contains another array. This functionality alone allows you to begin creating tables in Java, which means that using and interacting with documents like Excel becomes possible!

Multi-dimensional Array Example in Java

Expanding on the scores array idea, you can create a new array called scores_multi  with a second dimension, allowing several scores to be recorded.

/* scores_multi is an array of arrays. It has a length of 5,  
but each index contains another array of length 3
scores_multi is an array of arrays */
int[][] scores_multi = new int[5][3];

The array above will have a structure that looks like this (each box holding its index):

{ [0] [1] [2] }, //  [0]  
{ [1] [1] [2] }, //  [1]  
{ [2] [1] [2] }, //  [2]  
{ [3] [1] [2] }, //  [3]  
{ [4] [1] [2] } //   [4]  

How do the Functionalities Work in Multi-dimensional Arrays?

All the functionalities you saw for the 1D array apply to multi-dimensional arrays.

Array Index

To access elements in a multi-dimensional array, you must specify each index. Using an example array called tables populated with the following values, you can access each element using the right and left index.

int[][] tables = {
  {67, 45},
  {987, 234}
};

The array tables is an array of length 2, with each index containing an array of size two. To access the value 987, you must specify its location using the right and left indexes.

tables[1][0]; //returns the value 987

Given: int[][] a = new int[2][3]; The resulting 2D array would look like the image below.

infographic demonstrating structure of 2D array - much like a spreadsheet

The values in the squares are the indexes of each square. For instance, to get the int from the first row in the first column, it would be, int x = a[0][0];

Array Length

Getting the length of a multi-dimensional array is the same syntax as for a single-dimensional array. However, each level will return its respective length; if you don't provide an index, then the length attribute will automatically choose the first dimension.

For example, in the scores_multi array, if you call scores_multi.length, it will return 5, the length of the first dimension. If you want the length of the second dimension, then you can call the length of the first value of the array: scores_multi[0].length, which will return 3.

This is because when you call the length variable on the array, it counts the first list of values or the outer loop, and it doesn't take into account whether the value is a number or an array. When you call the length variable on any value of this outer loop, the length attribute will now see that it is an array and count each value within this array or the inner loop. Remember, a 2-dimensional array is an array of arrays.

Access 2D Arrays with For Loops

The for loop is the best way to iterate through single- and multi-dimensional arrays. This is because for loops have a built-in index you can use. For example:

class Main {
  public static void main(String[] args) {
    int[][] twoD = new int[4][7];

    for (int i=0; i < twoD.length; i++) {
      for (int j=0; j < twoD[i].length; j++) {
        twoD[i][j] = i * j;
        System.out.print(twoD[i][j] + " - ");
      }

      // here you'll print out a new line
      System.out.println();
    }
  }
}

Notice that in the declaration of the inner for loop, you use j < twoD[i].length, whereas in the outer loop, you just use i < twoD.length, as explained in the length section above.

Irregular Arrays

Something new with multi-dimensional Arrays is the possibility of creating irregular arrays, which are multi-dimensional arrays of non-uniform size. For example, the following array is valid:

int[][] irreg_array = {
  {546, 23},
  {123, 345, 87},
  {345, 87, 907, 45}
};

Experiment with Arrays

Use the code editor below to experiment and practice using Arrays. Please demonstrate how to:

  • Declare and initialize a simple array
  • Populate the array
  • Access individual values within the array
  • Print each element of the array using a for loop
class Main {
  public static void main(String[] args) {
    // write your code below this line



    // keep your code above this line
  }
}

Experiment with Multi-Dimensional Arrays

In the code editor below, please write the code to:

  • demonstrate how to declare a two-dimensional Array with a height of 4 and a width of 5
  • populate every index in the 2-D Array using a nested for loop
  • print each element in the 2-D array using a separate nested for loop
class Main {
  public static void main(String[] args) {
    // write your code below this line
    

    // keep your code above this line
  }
}

Summary: Java Multi-dimensional Arrays

  • A multi-dimensional array is a collection of nested Arrays (or an array of arrays)
  • Multi-dimensional Arrays allow you to make tables in Java
  • multi-array[0][1] is an index example for a 2D multi-dimensional array
  • The length attribute automatically calls the 1st level of the array unless an index is provided
  • Nested for loops are perfect for looping through nested arrays
  • Irregular arrays are valid in Java