Arrays
What are they?
- Arrays are a series of variables of the same type related
by the same name. Each variable in the series is accessed
by its index location. The indexes run from 0 to size - 1,
where size is the total number of variables or positions in
the array. If an array is of size 5, there are 5 positions
(0 to 4) in the array starting with 0, the second position
is 1, the third position is 2, the fourth position is 3, the
fifth position is 4.
Creating Arrays
- When Creating an array there are two things that must be
consided. What type will the array be and what size will the
array be (Note that for non-primitive types the creation of
the array has an extra step).
Primitive Types
- There are two steps that need to be followed when creating
an array of a primitive type.
- Declare the Array - state what type the array is and
what the name of the array is.
- Create the Space for the Array - state what the size
of the array is, so that the computer reserves the
space in memory.
- Here are some examples creating primitive type arrays, note that the
declaration is on the left side of the equal sign and the
creation of space is on the right side of the equal sign.
The variable size is declared to be type int, is initialized,
and corresponds to the size of each array.
char word[] = new char[size];
boolean tFValues[] = new boolean[size];
int mediumNumbers[] = new int[size];
long bigNumbers[] = new long[size];
double largeDecimals[] = new double[size];
- Here is how the creation can be broken up into the two steps
on different lines.
- char word[];
- word = new char[size];
- int mediumNumbers[];
- mediumNumbers = new int[size];
- long bigNumbers[];
- bigNumbers = new long[size];
- double largeDecimals[];
- largeDecimals = new double[size];
Non Primitive Types
- There are three steps that need to be followed when creating
an array of non-primitive types.
- Declare the Array - state what type the array is and what the name
of the array is (the same as for a primitive type).
- Create the Space for the Array - state what size
the array is, so that the computer reserves the
space in memory (the same as for a primitive type).
- Create Each Individual Object in the Array - since an
array is a series of variables of the same type,
there will be a series of objects in a non-primitive
type array. Each of these objects must be created.
- Here is an example using the Circle type.
Circle c[] = new Circle[10];
Just as with the primitive types this could be broken
into two lines:
- Circle c[];
- c = new Circle[10];
Now each individual object must be created. The easiest way
to do this is to use a loop:
for (i = 0; i < 10; i++) {
c[i] = new Circle();
}
- Here is an example using the String type.
String str[] = new String[25];
Just as with the primitive types this could be broken
into two lines:
- String str[];
- str = new String[25];
Now each individual object must be created. The easiest way
to do this is to use a loop:
for (i = 0; i < 25; i++) {
str[i] = new String();
}