The purpose of this exercise is:
![]() |
|
Arrays are an important feature of many languages; sometimes, we don't want to give a new label to each variable that we store. Maybe, we don't know in advance how many we are going to need. This is where arrays come in.
You can read about arrays in the Java Tutorial. As with most of the rest of the exercises, we are going to investigate this by animating something.
![]() |
public void animateColourCircle(){ Circle c = new Circle(); c.makeVisible(); Colour[] colours = Colour.values(); }
public static void main( String[] args ) throws InterruptedException { ColourChange c = new ColourChange(); c.setAnimateStep( 500 ); c.animateColourCircle(); Thread.sleep( 2000 ); System.exit( 0 ); }
|
![]() | If you get odd things happening in the last section, consider carefully the names of your variables. |
You probably wrote a for loop which looked something like this:
for( int i = 0; i < 6; i++ ){ // stuff in here }
Which works fine.
![]() | Try changing the 6 to a 10. |
Now the code crashes; you should get something like this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at animate.ColourChange.animateColourCircle(ColourChange.java:25) at animate.ColourChangeTest.main(ColourChangeTest.java:18)
Again, the Java tutorial explains a great deal about exceptions and why we would want to use them.
We can use them to build an alternative for
loop if we choose; this is not
particularly recommended but demonstrates a point. It also shows usage of a
while
loop.
![]() |
Replace the for loop with this code:
// strange mechanism for doing a for loop. // this code is NOT recommended for general use. try{ int i = 0; while (true) { c.changeColour(colours[i]); animateStep(); i++; } } catch ( ArrayIndexOutOfBoundsException abe ){ // carry on... // but at least print something out because ignoring // exceptions is very bad practice. System.out.println( "Reached end of array" ); } |
![]() |
|
Although this example is a little pathological, exceptions are very useful. They tell you what sort of things could go wrong, and give you a chance to deal with them; handling errors is critical in programming.
At times, most programmers get irritated with exceptions. NullPointerException
and IOException
are some of the most common in Java.
for
loopOne characteristic of an array is that they are objects that know how long they are. One way of improving our for loop is to use this characteristic. To get the length of an array, we use a call like this:
colours.length;
![]() |
|
Now your code should run again; importantly, we could add a new Colour
and the
code would still work correctly. In fact, this pattern used to be the standard
way of accessing all the elements of an array. The Java language has now been
updated and there is a better way still, which is called the foreach
loop 1.
![]() |
Try this instead:
for( Colour colour: colours ){
}
|
![]() |
|
![]() |
The final version of ColourChange.java is the coursework for this exercise.
|
1. Why foreach
? Well, many languages use a keyword called foreach
to make
this form of loop. When Java was updated they choose not to. This
avoided introducing a new keyword which could have broken older
programs.