2D and Multi-Dimensional Arrays#

In Java, we can make multidimensional arrays, simply by putting a 1D array into a 1D array, to get a 2D array, and then putting a 1D array into a 2D array to get a 3D array, etc. Here are some examples of 2D arrays:

  1. A 5 X 2 array of real numbers, with the first column initialized to 0.5 and the second column initialized to 3.5:

    double[][] myArray = {{0.5, 3.5}, {0.5, 3.5}, {0.5, 3.5}, 
                          {0.5, 3.5}, {0.5, 3.5}};
    






  2. A 10 X 10 array of integers, initialized to 0’s on creation with new and then setting the top-left and bottom-right values to 20.

    int[][] thisArray = new int[10][10];
    thisArray[0][0] = 20;
    thisArray[9][9] = 20;
    





Practice Questions#

  1. Make a 9 X 9 array of characters and set the entire array to the hash ‘#’, except the very middle which should be your first initial, capitalized.

  2. Make a 3 X 4 X 5 array of integers and set the corners to 100.

To Solutions