Array of Color Objects

  1. Declare the reference, or array variable.
    Color	colorList[]; 
    The square brackets indicate that the variable called colorList is an array of class Color. You can also put the brackets before the identifier name.
     Color	[] colorList; 
    If you wish to declare multiple arrays, each one must have its own set of brackets.
    
    Color    colorList1[], colorList2[];	// declares 2 arrays
    Color[]  colorList3, colorList4;	// declares 1 array and 1 Color object?? Or is it 2 arrays?
    Color    colorList5[], colorList6;	// declares 1 array and 1 Color object
    
    - In this example code, colorList1, colorList2, colorList3,and colorList5 all have a data type of array of Color.
    - The colorList6 variable has a data type of Color because it doesn't have the square brackets.
    - The datatype for colorList4 is unclear - try it to see if you can figure out the data type, but this syntax is highly discouraged.

  2. Allocate the slots for the array with new.
    colorList1 = new Color[3]; 
    The number in the square brackets indicates how big the array is. You can access the size of the array with the length member variable. This code will printout the number 3.
    System.out.println(colorList1.length); 
    You can combine the first two steps into one statement of code.
    Color colorList2[] = new Color[5]; 
    This declaration creates the colorList2 array object amd allocates the array to hold up to 5 Color objects. Note that although it is one statement, both Step 1 (declare the array variable) and Step 2 (allocate the slots) are being done.

  3. Create the elements in the array with new.