Imagine that you want to use a multidimensional array to represent a multiplication table:
Each of the pairs of square brackets represents one dimension, so this is a two-dimensional array. To access a single int element of this two-dimensional array, you must specify two index values, one for each dimension. Assuming that this array was actually initialized as a multiplication table, the int value stored at any given element would be the product of the two indexes. That is, products[2][4]would be 8, and products[3][7] would be 21.int[][] products; // A multiplication table
To create a new multidimensional array, use the new keyword and specify the size of both dimensions of the array. For example:
In some languages, an array like this would be created as a single block of 100 int values. Java does not work this way. This line of code does three things:int[][] products = new int[10][10];
- Declares a variable named products to hold an array of arrays of int.
- Creates a 10-element array to hold 10 arrays of int.
- Creates 10 more arrays, each of which is a 10-element array of int. It assigns each of these 10 new arrays to the elements of the initial array. The default value of every int element of each of these 10 new arrays is 0.
The new keyword performs this additional initialization automatically for you. It works with arrays with more than two dimensions as well:int[][] products = new int[10][]; // An array to hold ten int[] values. for(int i = 0; i < 10; i++) // Loop ten times... products[i] = new int[10]; // ...and create ten arrays.
When using new with multidimensional arrays, you do not have to specify a size for all dimensions of the array, only the leftmost dimension or dimensions. For example, the following two lines are legal:float[][][] globalTemperatureData = new float[360][180][100];
The first line creates a single-dimensional array, where each element of the array can hold a float[][]. The second line creates a two-dimensional array, where each element of the array is a float[]. If you specify a size for only some of the dimensions of an array, however, those dimensions must be the leftmost ones. The following lines are not legal:float[][][] globalTemperatureData = new float[360][][]; float[][][] globalTemperatureData = new float[360][180][];
Like a one-dimensional array, a multidimensional array can be initialized using an array literal. Simply use nested sets of curly braces to nest arrays within arrays. For example, we can declare, create, and initialize a 5×5 multiplication table like this:float[][][] globalTemperatureData = new float[360][][100]; // Error! float[][][] globalTemperatureData = new float[][180][100]; // Error!
Or, if you want to use a multidimensional array without declaring a variable, you can use the anonymous initializer syntax:int[][] products = { {0, 0, 0, 0, 0}, {0, 1, 2, 3, 4}, {0, 2, 4, 6, 8}, {0, 3, 6, 9, 12}, {0, 4, 8, 12, 16} };
When you create a multidimensional array using the new keyword, you always get a rectangular array: one in which all the array values for a given dimension have the same size. This is perfect for rectangular data structures, such as matrixes. However, because multidimensional arrays are implemented as arrays of arrays in Java, instead of as a single rectangular block of elements, you are in no way constrained to use rectangular arrays. For example, since our multiplication table is symmetrical about the diagonal from top left to bottom right, we can represent the same information in a nonrectangular array with fewer elements:boolean response = bilingualQuestion(question, new String[][] { { "Yes", "No" }, { "Oui", "Non" }});
When working with multidimensional arrays, you'll often find yourself using nested loops to create or initialize them. For example, you can create and initialize a large triangular multiplication table as follows:int[][] products = { {0}, {0, 1}, {0, 2, 4}, {0, 3, 6, 9}, {0, 4, 8, 12, 16} };
int[][] products = new int[12][]; // An array of 12 arrays of int. for(int row = 0; row < 12; row++) { // For each element of that array, products[row] = new int[row+1]; // allocate an array of int. for(int col = 0; col < row+1; col++) // For each element of the int[], products[row][col] = row * col; // initialize it to the product. }
No comments:
Post a Comment