|
Here is an illustration of the memory map for the following code fragment, which uses a one-dimensional and a two-dimensional array of objects. Notice how the objects are not "in" the arrays. The array elements actually hold references to the objects. This is different than an array of primitive data types (like
// create a one-dimensional array of Color objects
Color[] myArray = new Color[4]; // an array of 4 Color references
// now create the objects for the array of objects
myArray[0] = new Color(12, 21, 0);
myArray[1] = new Color( 0, 12, 21);
myArray[2] = new Color(12, 0, 21);
myArray[3] = myArray[0]; // reuse one of the previous elements
// create a two-dimensional array of Color objects
Color[][] myArray2 = new Color[3][2]; // an array of 6 Color references
// now create the objects for the array of objects
myArray2[0][0] = new Color(56, 65, 0);
myArray2[0][1] = new Color( 0, 56, 65);
myArray2[1][0] = new Color(56, 0, 65);
myArray2[1][1] = myArray2[0][1]; // reuse one of the previous elements
myArray2[2] = myArray2[0]; // reuse one of the previous rows
|