Some Background on Multidimensional Arrays

 

Introduction.  We have been working with what are called one-dimensional arrays.  Now we need to develop a bit of background on arrays with more indices which are called multidimensional arrays.

When I first studied arrays using Fortran back in 1972 they weren't all that much different than what we are doing presently.  Though I want to mention a couple things that were different from arrays in Java.  One thing about arrays was they were subscripted in the form arrayName(i,j) or arrayName(i,j,k). Java will be somewhat different.  Another example of something that was different back then was that every row of a two dimensional array needed to have the same number of elements.  We will not need this restriction in Java.  But rather than continue with these sorts of  teaching through something that is no longer around I want to get to multidimensional arrays in Java.

In Java, multidimensional arrays are represented using square braces that are distinct for each dimension.  So the elements of a multidimensional array are specified by the sequence displayed in the following table.

 

dimensions subscripting
1 arrayName[ i ]
2 arrayName[ i ][ j ]
3 arrayName[ i ][ j ][ k ]
   
n arrayName[ i ][ j ] . . . [ m ]

 

This sort of segmentation of the subscripts is justified by a number of reasons.  For example, each row in a two dimensional array in Java does not need to have the same number of elements.

Carrying on with part of one of our earlier examples gives the following string array.

 

orb[0][0] = "Mercury"    
orb[1][0] = "Venus"    
orb[2][0] = "Earth" orb[2][1] = "Moon"  
orb[3][0] = "Mars" orb[3][1] = "Phobos" orb[3][2] = "Deimos"

 

Notice that every row does not have the same length.  For your elucidation and reference you should attempt to get comfortable with the following.
  • orb.length = 4
    • one for each planet
  • orb[0].length = 1
    • since there is only Mercury in this "row"
  • orb[2].length = 2
    • since there is an entry for Earth and Moon in this "row"
  • orb[3].length = 3
    • since there is an entry for Mars, Phobos and Deimos in this "row"

We will put these into a program in the next webpage where we will also address initializing multidimensional arrays.