Special Effects and Game Development
in Java(TM) - Arrays
by Anibal Wainstein
4.0.3 Arrays
Now we have come to the point where we cannot work without
arrays no longer. An array is a group of variables that are
all of the same type. You can compare arrays with a cabinet
and its drawers. Imagine that you have a drawer that represents
a variable, the drawer is part of a cabinet together with
other identical drawers. The cabinet that contains the drawer
is then the array. A variable in an array is called element.
When you initialize an array you must always specify how many
variables it will contain. This is done with the new
keyword. You can also specify the number of dimensions an
array will have, but we will not review that here. Imagine
now that we want to create an array with 5 integer numbers,
then first we declare an array of integers:
int myarray[]; The two brackets
[] are used to indicate that the variable "myarray"
is an array. Now we must initialize the array and specify
how many elements it will contain:
myarray = new int[5]; The array "myarray"
now contains 5 integers, you can access them by writing:
int firstelement=myarray[0]; int secondelement=myarray[1]; int thirdelement=myarray[2]; int fourthelement=myarray[3]; int lastelement=myarray[4];
The number within the brackets is pointing on the first element
in the array and it is called an index. Please note
that arrays are always zero based, which means that the first
element is accessed by specifying zero (and not 1 as one may
think). In the example above we access the last integer by
writing a 4. So 4 is the maximum for the index number even
though the size of the array is 5. You can also initialize
and declare the size for an array in a single line:
int myarray[] = new int[5];
You may now think that you know everything you need to know
about arrays? Not yet... Please note that what we have declared
so far have not had classes as elements. Now we will look
at the process of creating arrays of classes. Assume that
we want to create an array of 30 String objects:
String mystrings[] = new String[30];
It looks the same as the example above but "int"
has been exchanged for "String" instead! Yes, of
course! But this does not mean that we are finished yet. What
you have managed to create are just an array with references.
Now you must initialize each unique array element:
mystrings[0]=new String("String 0");
mystrings[1]=new String("String 1");
mystrings[2]=new String("String 2");
...
mystrings[29]=new String("String 29");
It looks worksome, right? Well, you get used to it! Sometimes
you can find smart ways to make this element initializing
automatic. The example above can be written like this instead:
String mystrings[]=new String[30]; for (int i=0; i<30; i++)
{
mystrings[i]=new String("String "+i);
}
Please note that if you try to add an integer, float or double
to a String then the number will be automatically converted
to a String object before being added. I have used this trick
above in the initialization of the string ("String "
+ i). A great relief is that arrays with integers and floating
point numbers always have the value zero, at the beginning,
so these do not have to be initialized if you happen to depend
on that.
Next Page >>
|