Looping through an array
22 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)To visit every element, run a for loop from index 0 up to length - 1. The loop counter is the index, so marks[i] is the element for the current round.
The standard loop
Printing 5 marks with 5 println lines stops working at 60 marks. Let a for loop (opens in a new tab) count the index from 0 while i < marks.length; each round, marks[i] is the next element.
- Line 1: use <array>[i]
Example 1 · Print every mark with its subject number
public class PrintMarks { public static void main(String[] args) { int[] marks = {78, 85, 62, 90, 71}; for (int i = 0; i < marks.length; i++) { System.out.println("Subject " + (i + 1) + ": " + marks[i]); } }}Subject 1: 78 Subject 2: 85 Subject 3: 62 Subject 4: 90 Subject 5: 71
What this code does
igoes 0, 1, 2, 3, 4, andmarks[i]is the mark for that round.- People count subjects from 1, so the program prints
i + 1as the subject number and keepsias the index. - When
ibecomes 5,5 < marks.lengthis false and the loop stops before reading outside the array.
Use marks.length, not a fixed 5, so the loop stays right when the array grows. And use <, not <=: with <= the last round asks for marks[5], which is outside the array. Why not just print the array? System.out.println(marks) does not print the marks: it prints something like [I@1b6d3586 ("int array" and a memory code). Loop over the array to print each element, or use Arrays.toString(marks) (opens in a new tab) later in this module.
Looping backwards
A weather app shows the latest day first. Start the counter at the last index, temps.length - 1, count down with i--, and keep going while i >= 0 so index 0 is included.
Trace the code
Latest day first
Variables
- temps
- {31, 33, 30}
Output
(nothing printed yet)Line 3Three days of temperatures; the latest day, 30, is at the end.
Filling an array with a loop
A scorer types the runs of each over, and the program must keep them. Make the array with new, then assign to runs[i] inside the loop; the value can come from the user or from a calculation.
Example 2 · Fill an array from the keyboard
import java.util.Scanner;
public class OverRuns { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] runs = new int[3]; for (int i = 0; i < runs.length; i++) { System.out.print("Runs in over " + (i + 1) + ": "); runs[i] = sc.nextInt(); } System.out.println("Over 2 had " + runs[1] + " runs"); sc.close(); }}Runs in over 1: 6 Runs in over 2: 11 Runs in over 3: 4 Over 2 had 11 runs
What this code does
- Each round asks for one number and stores it in
runs[i]with Scanner (opens in a new tab). - The sample shows the user typing 6, 11 and 4. After the loop, all three values are in the array, ready to use.
A calculation works the same way: table[i] = 7 * (i + 1); fills a new int[5] with 7, 14, 21, 28 and 35.
Picking some elements
The coach wants only the overs with 10 or more runs. Put an if (opens in a new tab) inside the loop: every element is visited, but only the ones that pass the check are printed.
Example 3 · Only the big overs
public class BigOvers { public static void main(String[] args) { int[] runs = {6, 12, 4, 15, 9, 10}; for (int i = 0; i < runs.length; i++) { if (runs[i] >= 10) { System.out.println("Over " + (i + 1) + ": " + runs[i]); } } }}Over 2: 12 Over 4: 15 Over 6: 10
What this code does
- The loop visits all 6 overs; the
iflets only 12, 15 and 10 through. - The over number comes from the index,
i + 1, so the loop needs an index even though most rounds print nothing.
To visit every second element instead, change the update: i += 2 gives the indexes 0, 2, 4 and so on.
Next step
Check your understanding
9 quick questions