Arrays
Lesson:
Main Points:
- Java arrays can contain primitives, object references, or other
arrays.
- There are three steps to create and use an array: declaration,
construction, and initialization.
- Java arrays begin with index 0.
- 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 Type | Initial Value |
byte | 0 |
int | 0 |
float | 0.0f |
char | '\u0000' |
short | 0 |
long | 0L |
double | 0.0d |
boolean | false |
object reference | null |
There are four basic approaches to initialization:
- Go with default values
- Separately initialize each element as in len[4]=4.5;
- Initialize elements in a loop. (see the sample applet)
- 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
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.