Java Reference
In-Depth Information
The 21 elements in the array will occupy 84 bytes. When you need a two-dimensional array with rows
of varying length, allocating them to fit the requirement can save a considerable amount of memory
compared to just using rectangular arrays where the row lengths are all the same.
To check out that the array is as shown, you could implement this in a program, and display the
length member for each of these arrays.
Multi-dimensional Arrays
You are not limited to two-dimensional arrays either. If you are an international Java Bean grower with
multiple farms across several countries, you could arrange to store the results of your bean counting in
the array declared and defined in the statement:
long[][][] beans = new long[5][10][30];
The array, beans , has three dimensions. It provides for holding bean counts for each of up to 30 fields
per farm, with 10 farms per country in each of 5 countries.
You can envisage this as just a three-dimensional array, but remember that beans is an array of five
elements, each of which holds a two-dimensional array, and each of these two-dimensional arrays can be
different. For example if you really want to go to town, you can declare the array beans with the statement:
long[][][] beans = new long[3][][]; // Three two-dimensional arrays
Each of the three elements in the first dimension of beans can hold a different two-dimensional array,
so you could specify the first dimension of each explicitly with the statements:
beans[0] = new long[4][];
beans[1] = new long[2][];
beans[2] = new long[5][];
Search WWH ::




Custom Search