Arrays


x

Lesson:
Main Points:
  1. Java arrays can contain primitives, object references, or other arrays.
  2. There are three steps to create and use an array: declaration, construction, and initialization.
  3. Java arrays begin with index 0.
  4. Elements stored in arrays are addressed through use of an index number contained within square brackets.

Declaration:
Declaration tells the compiler the name of the array and the type its elements will be. Examples:
  int numbers[];  // an array containing integers
  String words[];  // an array containing strings
  Color clist[];  // an array containing colors
  double[] len; // an array of doubles
The square brackets can come before or after the array name.

Construction:
The size of an array is not specified in the declaration. The size is specified during the construction phase like this:

  numbers = new int[20];
  words = new String[30];
  clist = new Color[size]; //where size is an integer value
  len = new double[10];

Optionally, declaration and construction can be combined like this:
  int numbers[] = new int[20];
  double[] len = new double[10];

Initialization:
When an array is constructed, its elements are automatically initialized exactly as for object member variables. Numerical elements are initialized to zero; non-numerical elements are initialized to values similar to zero as shown in the following table.
Element TypeInitial Value
byte0
int0
float0.0f
char'\u0000'
short0
long0L
double0.0d
booleanfalse
object referencenull

There are four basic approaches to initialization:
  1. Go with default values
  2. Separately initialize each element as in len[4]=4.5;
  3. Initialize elements in a loop. (see the sample applet)
  4. Combine declaration, construction, and initialization. (see the sample applet)


import java.awt.*; import java.applet.*; public class ex05 extends Applet{ //declaration, construction, and initialization String[] names = { "Bobby", "Mildred", "Gertrude", "Harry", "Sam", "Wilbur" }; //declaration and construction int[] numbers = new int[6]; public void init(){ setBackground(Color.blue); //initialization in a loop for(int i=0; i<names.length; i++){ numbers[i]=2+2*i; } } public void paint(Graphics g){ g.setColor(Color.white); for(int i=0; i<names.length; i++){ g.drawString(names[i]+" gets "+numbers[i]+" cookies.", 10, 15+i*15); } } }
x

Assignment:
The student will create an applet which contains an array of six different colors, an array of six sentences, and an array of six numbers. The students may use any method of declaration, construction, and initialization he or she wishes to. Output will look something like this:
-----------------------------------------------------
1. There should be six sentences.
2. Each sentence will be a different color.
3. The sentences will be spaced out evenly.
4. The color values will be supplied by the color array.
5. The numbers will be supplied by the int array.
6. The sentences will be supplied by the String array.
-------------------------------------------------------

Note: You may have to use the drawString method like this:

  g.drawString(""+num[i]+". "+sent[i], 10, 20);
It is necessary to START a string in drawString with a string value and the "" is a way of dealing with situations where you want it to begin with value stored in an int.


Display your applet and its code on a web page.
x