|
Java Tutorial |
ArraysAndLoopsThese pages make up the course notes for my Java programming course (run at Dallam, Milnthorpe, Cumbria). Hopefully they also make a useful self-learning tutorial. It turns out that people are quite bad at handling lists of things, but computers are pretty good at it. The computer never gets distracted, or bored and if we ask it to remember a list of 50 numbers and then add them up it will not forget any, or make a mistake (you might make a mistake in writing the program and therefore the answer may not be correct, but the computer will not be mistaken – infuriating things, aren’t they?). The most basic way of handling lists of things is in an array. A variable is a slot into which we can place a single value, an array is a number of slots into which we can place a number of values. Think of an array as being like a set of pigeon holes, the first one is numbered 0 the second 1 the third 2 etc. To declare an array we have a few options:
float[] items = new float [10];
float [] moreItems = {4.99f, 7.99f, 9.99f };
The first example above creates a new array for holding floating point numbers with 10 empty slots (pigeon holes). The second example creates a new array for holding floating point numbers with 3 slots and assigns the values 4.99, 7.99 and 9.99 to them. To access the values in an array we use the array name plus the slot number we want e.g. items[0] = 10; //remember that the first slot has number 0 not 1 items[1] = 20; items[2] = moreItems[2]; Notice that in all these examples we are using square [] brackets. In the section on control we mentioned looping, loops come in a few flavours, for, while and do-while. At the moment we are only going to consider the for loop. A for loop allows us to alter the value of a variable each time we go round the loop until we hit another value. Commonly we want to count up or down a known number of times. Guess what, it turns out that a for loop and an array were made for each other! A for loop statement looks like this:
for( count = 0; count < 3; count=count+1) {
//do something
}
what this would do is set the value of the count variable to 0 and execute any code inside the braces. Then it would then loop round again, add 1 to count and check if count is less than 3 and loop again. This time the value of the count variable would equal 1, next time 2 and finally 3 at which point count would not be less than 3 so the code inside the for block would not be executed. Lets modify our
float item1 = 4.99f; //cost of item 1
float item2 = 7.99f; //cost of item 2
float item3 = 9.99f; //cost of item 3
float vatRate = 17.5f; //VAT as a percentage
//add up the items to get the net price
float netPrice = item1 + item2 + item3;
System.out.println("The net price is " + netPrice);
To be
float[] items = {4.99f, 7.99f, 9.99f};
float vatRate = 17.5f; //VAT as a percentage
//add up the items to get the net price
int count;
float netPrice = 0;
for (count=0; count<3; count=count+1) {
netPrice = netPrice + items[count];
}
System.out.println("The net price is " + netPrice);
What we have done here is replace the three separate item variables (item1, item2, item3) with an array that contains the three values. A little further down we are then using a for loop to work through the array for slots 0,1 & 2 and add up the values. What happens if you add more values to the array? HINT This work is Copyright Chris Hunter 2007, you may use it for non-commercial purposes |