Java Reference
In-Depth Information
Figure 2.7 shows the memory of the names array. The names array consists of 10 array
references, each pointing to an array of 5 array references, each pointing to an array of 20
String references for a total of 1,000 String references.
FIGURE 2.7
Multidimensional arrays in Java.
0
null
null
0
1
.
.
.
1
2
3
4
.
.
.
null
19
0
null
null
0
1
.
.
.
1
0
1
2
.
.
.
2
3
4
names
.
.
.
.
.
.
19
“George
Washington”
0
A String[ ][][ ] reference
1
Fifty arrays of
String references,
each of length 20
2
3
4
9
An array of 10
String[ ][ ] references
Ten arrays of String[ ]
references, each of
length 5
As you can see in Figure 2.7, the structure of multidimensional arrays in Java allows for
various column lengths. For example, the following statements are valid:
4. GregorianCalendar [][] months = new GregorianCalendar[12][];
5. months[0] = new GregorianCalendar[31];
6. months[1] = new GregorianCalendar[29];
7. months[3] = new GregorianCalendar[30];
The months array has 12 rows, the fi rst row is length 31, the second row is length 29, and
the fourth row is length 30. The rest of the rows could be initialized in this same fashion.
Array Initializers
An array initializer is a shorthand notation for declaring an array and fi lling it with values,
all in a single statement. Array initializers are convenient for quickly creating smaller
arrays. Instead of using the new keyword, you list the elements of the array in curly braces
separated by commas.
 
Search WWH ::




Custom Search