Arrays of Arrays

We'll discuss how the terminology of arrays is not universally consistent among all programming languages. Java uses the terminology consistently, though. Then we'll show how to declare arrays of arrays in Java.

Array terminology

The language specification says there are no multidimensional arrays in Java, meaning the language doesn't use the convention of Pascal or Ada to put several indexes into one set of subscript brackets. Ada allows multidimensional arrays like this:


year : array(1..12, 1..31) of real;          Ada code for
                                       multidimensional array.

year(i,j) = 924.4;

Ada also allows arrays of arrays, like this:


type month is array(1..31) of real;          Ada code for array of
                                       arrays.

year : array(1..12) of month;
year(i)(j) = 924.4;

Perhaps Ada and Pascal are a b it obscure these days, but I wanted to show you the different terminology and different design choices these languages have made, compared with Java. In some cases the terminology in one language is in conflict with the terminology in another language. Make sure everyone has a consistent view of the terminology before you start designing your software.

Declaring arrays of arrays

Java arrays of arrays are declared like this:

Fruit plums [] [] ;

Array “plums” is composed of an array of elements each of which is an array whose elements are Fruit objects. You can allocate and assign to any arrays individually.

plums = new Fruit [23] [9]; // an array[23] of array[9]
plums [i] = new Fruit [17]; // an array[17]
plums [i][j] = new Fruit(); // an individual Fruit

Because object declarations do not create objects (I am nagging about this repeatedly—it's an important point for bug free programs), you will need to fill out or instantiate the elements in an array before using it. If you have an array of arrays, like the one in the following example, you will need to instantiate both the top-level array and at least one bottom-level array before you can start storing ints:

int cabbage[][];

The bottom-level arrays do not have to all be a single uniform size. Here are several alternative and equivalent ways you could create and fill a triangular array of arrays:

  • Use several array creation expressions, like this:

    int myTable[][] = new int[][] {
                                  new int[] {0},
                                  new int[] {0,1},
                                  new int[] {0,1,2},
                                  new int[] {0,1,2,3},
                        };

  • Lump all the initializers together in a big array initializer, like this:

    int myTable[][] = new int[][] {
                        {0},
                        {0,1},
                        {0,1,2},
                        {0,1,2,3}, };

  • Initialize individual arrays with array creation expressions, like this:

    int myTable[][] = new int[4][];
    // then in statements
    myTable[0] = new int[] {0};
    myTable[1] = new int[] {0, 1};
    myTable[2] = new int[] {0, 1, 2};
    myTable[3] = new int[] {0, 1, 2, 3};

  • Use a loop, as outlined in the next section.

Looping over arrays

image

JDK 1.5 introduced a new form of the “for” loop, intended for iterating over an entire array. You provide three names: the element type, a name for the loop variable that will hold successive elements, and the array that you want to get elements from. Looping over a single array looks like this:

public static void main(String[] args) {
for(String s : args) {
    System.out.println(s);
}

Iterating through an array of arrays is similar. Say we have this array:

int myTable[][] = new int[][] {
                              new int[] {0},
                              new int[] {0,1},
                              new int[] {0,1,2},
                              new int[] {0,1,2,3},
                    };

We can loop over it with the following code. The outer loop has an element type of “array of int”, and the inner loop has an element type of int.

for (int[] mt : myTable) {
    for (int i : mt) {
        System.out.print( i + " ");
    }
    System.out.println();
}

Put it in a main() method in a class, and compile the code with:

javac -source 1.5 arr.java

Run the code to get this output:

java arr
0
0 1
0 1 2
0 1 2 3

More about arrays of arrays

If you don't instantiate all the dimensions at one time, you must instantiate the most significant dimensions first. For example:

int cabbage[][] = new int[5][];   // ok
int cabbage[][] = new int[5][3];  // ok

but:

int cabbage[][] = new int[][3];   // NO! NO! NO! (bad allocation order)

Arrays with the same element type, and the same number of dimensions (in the C sense, Java doesn't have multidimensional arrays) can be assigned to each other. The arrays do not need to have the same number of elements because (as you would expect) the assignment just copies one reference variable into another. For example:

int eggs[] = {1,2,3,4,5};
int ham[] = new int[2] {77, 96};
ham = eggs;
ham[3] = 0;    // OK, because ham now has 5 elements.

This doesn't make a new copy of eggs; it makes ham and eggs reference the same array object.

Watch the size of those arrays of arrays. The following declaration allocates an array of 4 * 250 * 1000 * 1000 bytes = 1GB.

int bubba[][][] = new int[250][1000][1000];

Do you have that much memory on your system? In 1998, that was an hilarious joke. In the great post-bubble memory glut of 2001, 1GB was about $120 worth of synchronous 133MHz DRAM. By Spring 2004, 1 GB of DRAM cost about $150, but it was on two chips not four, and ran at 333 MHz not 133MHz. This is the power of Moore's Law. (And I have always admired the way Gordon Moore had exactly the right last name to give to his observation. Moore's law does indeed give you “More and more”).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.71