Sub topic 4
Traversing Arrays
Traversing an array means visiting its elements one at a time. An array is a fixed-size group of values. Each value is an element, and each element has an index, or position number. Indexes start at zero.
Traversal is useful when you want to print every value, add values together, or update values. A loop repeats a set of instructions, so it can visit each element without a separate instruction for every position.
An indexed loop
This program visits the scores with an indexed loop, which uses an index to reach each element. It also shows another kind of loop and updates the scores.
Save it as Main.java. Compile it with javac Main.java, then run it with java Main.
public class Main {
public static void main(String[] args) {
int[] scores = {80, 90, 100};
int sum = 0;
for (int i = 0; i < scores.length; i++) {
System.out.println("Index " + i + ": " + scores[i]);
sum = sum + scores[i];
}
System.out.println("Total: " + sum);
for (int score : scores) {
System.out.println("Score: " + score);
}
for (int i = 0; i < scores.length; i++) {
scores[i] = scores[i] + 5;
System.out.println("Updated: " + scores[i]);
}
}
}The first loop prints the indexes and scores, then the program prints a total of 270. The next loop prints the same three scores. The last loop raises each score by 5 and prints 85, 95, and 105.
Look at the first loop:
- The variable i starts at 0, the index of the first element.
- The condition i < scores.length checks that i is less than the number of elements.
- The loop body, the instructions inside the braces, prints scores[i] and adds it to sum.
- After the body runs, i++ increases i by 1. The loop then checks the condition again.
For these three scores, the loop visits indexes 0, 1, and 2. When i becomes 3, the condition is false, so the loop stops.
If an array is empty, its length is 0. The condition is false at the start, so the loop body does not run.
An enhanced for loop
An enhanced for loop visits each element without giving you its index. In the example, the loop reads one value at a time from scores into the variable score. Read the colon as “in”: for each score in scores, print the score.
This form is useful when you need every value but do not need to know its position. It also does nothing for an empty array.
For an array of numbers, changing the loop variable score would not change the array element. The variable holds a copy of the number. To update an element, use an indexed loop and assign a new value to scores[i], as the last loop in the program does.
Choosing a loop
- Use an indexed loop when you need a position or want to replace an element.
- Use an enhanced for loop when you only need to read each value in order.
- Use the array’s length as the limit for an indexed loop, rather than counting elements yourself.
Exercise
An array has four elements. Which indexes does a loop visit if it starts at 0 and continues while i is less than the array length?
Show answer
It visits indexes 0, 1, 2, and 3.
Exercise
You want to add 5 to every number stored in an array. Which loop form lets you assign each result back to its element?
Show answer
An indexed loop. Use the index to assign the new value to each array element.