Recipe 7.15 Multidimensional Structures


Problem

You need a two-, three-, or more dimensional array or ArrayList.

Solution

No problem. Java supports this.

Discussion

As mentioned back in Recipe 7.1, Java arrays can hold any reference type. Since an array is a reference type, it follows that you can have arrays of arrays or, in other terminology, multidimensional arrays. Further, since each array has its own length attribute, the columns of a two-dimensional array, for example, do not all have to be the same length (see Figure 7-2).

Here is code to allocate a couple of two-dimensional arrays, one using a loop and the other using an initializer. Both are selectively printed:

/** Show Two-Dimensional Array of Objects */ public class ArrayTwoDObjects {     /** Return list of subscript names (unrealistic; just for demo). */     public static String[][] getArrayInfo( ) {         String info[][];         info = new String[10][10];         for (int i=0; i < info.length; i++) {             for (int j = 0; j < info[i].length; j++) {                 info[i][j] = "String[" + i + "," + j + "]";             }         }         return info;     }     /** Return list of allowable parameters (Applet method). */     public static String[][] getParameterInfo( ) {         String param_info[][] = {             {"fontsize",    "9-18",    "Size of font"},             {"URL",    "-",    "Where to download"},         };         return param_info;     }     /** Run both initialization methods and print part of the results */     public static void main(String[] args) {         print("from getArrayInfo", getArrayInfo( ));         print("from getParameterInfo", getParameterInfo( ));     }     /** Print selected elements from the 2D array */     public static void print(String tag, String[][] array) {         System.out.println("Array " + tag + " is " + array.length + " x " +              array[0].length);         System.out.println("Array[0][0] = " + array[0][0]);         System.out.println("Array[0][1] = " + array[0][1]);         System.out.println("Array[1][0] = " + array[1][0]);         System.out.println("Array[0][0] = " + array[0][0]);         System.out.println("Array[1][1] = " + array[1][1]);     } }

Figure 7-2. Multidimensional arrays
figs/jcb2_0702.gif


Running it produces this output:

> java ArrayTwoDObjects Array from getArrayInfo is 10 x 10 Array[0][0] = String[0,0] Array[0][1] = String[0,1] Array[1][0] = String[1,0] Array[0][0] = String[0,0] Array[1][1] = String[1,1] Array from getParameterInfo is 2 x 3 Array[0][0] = fontsize Array[0][1] = 9-18 Array[1][0] = URL Array[0][0] = fontsize Array[1][1] = -        >

The same kind of logic can be applied to any of the Collections. You could have an ArrayList of ArrayLists, or a Vector of linked lists, or whatever your little heart desires.

As Figure 7-2 shows, it is not necessary for the array to be "regular." That is, it's possible for each column of the 2D array to have a different height. That is why I used array[0].length for the length of the first column in the code example.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net