What is a 2-dimensional array ?

  • An one-dimensional array organizes the variables along a line:

                

  • A two-dimensional array organizes the variables along 2 (orthogonal) lines (or 2 dimensions):

                

Using multi-dimensional arrays to store data

  • An one-dimensional array is a data struture suitable to store a list of elements

    Example: cities

      "Atlanta"     "New York"     "San Francisco" 

  • A two-dimensional array is a data struture suitable to store a relationship between list of elements

    Example: the distance between cities

                     "Atlanta"  "New York" "San Francisco" 
     "Atlanta"           0         863          2482
     "New York"         863         0           2906
     "San Francisco"   2482       2906            0
      

Using multi-dimensional arrays to store data

  • Example: the distance between cities

                     "Atlanta"  "New York" "San Francisco" 
     "Atlanta"           0         863          2482         // Row 0
     "New York"         863         0           2906         // Row 1
     "San Francisco"   2482       2906            0          // Row 2
      

  • How we can use a 2-dim-array to store distance information between cities:

            double[][] distances = {
                                    {0, 863, 2482},     // Row 0
                                    {863, 0, 2906},     // Row 1
                                    {2482, 2906, 0}     // Row 2
                                   };
     

Accessing elements in a multi-dimensional arrays

  • A 2-dimensional array is accessed using 2 indexes:

          distances[index1][index2] 

  • The first index index1 is often called the row index

  • The second index index2 is often called the column index

    Because of the common representation of a 2-dimensional array is as follows:

      distances:   col 0    col 1    col 2    col 3
                 +--------+--------+--------+--------+
          row 0  | [0][0] | [0][1] | [0][2] | [0][3] | 
                 +--------+--------+--------+--------+
          row 1  | [1][0] | [1][1] | [1][2] | [1][3] |
                 +--------+--------+--------+--------+
          row 2  | [2][0] | [2][1] | [2][2] | [2][3] |
                 +--------+--------+--------+--------+ 
    
      1st index = the row index
      2nd index = the column index

Using multi-dimensional arrays to store data

  • Example of program code that prints out the elements in a 2-dimensional array:

        public static void main(String[] args)
        {
            double[][] distances = {  {0, 863, 2482},  // Row 0
                                      {863, 0, 2906},  // Row 1
                                      {2482, 2906, 0}  // Row 2
                                   };
                                   
            for (int i = 0; i < distances.length; i++)
            {
                for (int j = 0; j < distances[i].length; j++)
                    System.out.print(distances[i][j] + "  ");
                System.out.println();
            }
        } 

  • The variable distance.length contains the # rows in the 2-dimensional array
  • The variable distance[i] is the ith row in the 2-dimensional array
  • The variable distance[i].length contains the # elements (columns) in the ith row

DEMO: demo/09-multi-dim-array/01-intro/Demo.java