This lesson covers two additional techniques to manipulate and traverse arrays.
Store connected pieces of data using two (or more) arrays whose indices correspond, allowing us to treat the arrays together as a unified data structure.
Two arrays are considered related when element i in one array refers to data that matches element i in another.
This lets us keep separate lists (e.g., names and scores) but still treat each pair as a single “record.”
Typical use cases:
firstName[]andlastName[]players[]andscores[]products[]andprices[]countries[]andpopulations[]cities[]andtemperatures[]
When using related arrays:
- ensure both arrays have the same length
- always use the same index variable when traversing them
- use regular for loops, not enhanced for loops, because you need index access
String[] names = {"Ava", "Liam", "Noah", "Zoe"};
int[] grades = {85, 92, 77, 90};
for (int i = 0; i < names.length; i++) {
System.out.println(names[i] + " scored " + grades[i]);
}String[] products = {"Milk", "Eggs", "Bread", "Apples"};
double[] prices = {2.99, 3.49, 2.50, 4.25};
for (int i = 0; i < products.length; i++) {
System.out.println(products[i] + " costs $" + prices[i]);
}String[] countries = {"Canada", "Japan", "Brazil"};
int[] populationMillions = {38, 125, 214};
for (int i = 0; i < countries.length; i++) {
System.out.println(countries[i] + ": " + populationMillions[i] + " million");
}Related arrays let us keep different pieces of information connected by using the same index. This is useful when the data naturally comes in pairs or groups.
When applying the technique of related arrays, make sure to:
- keep arrays the same length
- always traverse them using the same index variable
Use a simpler loop structure when you only need to read each value in an array, one at a time, in order.
A regular for loop gives you the index, which lets you access positions, update values, and work with related arrays:
int[] marks = {72, 85, 90};
for (int i = 0; i < marks.length; i++) {
System.out.println("Index " + i + ": " + marks[i]);
}This prints:
Index 0: 72
Index 1: 85
Index 2: 90
In this form, you can use i for:
- finding the last element
- modifying an element (e.g.,
marks[i] = ...) - using the same index for two related arrays (e.g.,
names[i]andscores[i])
An enhanced for loop (also called for-each) automatically gives you each value, not the index:
int[] marks = {72, 85, 90};
for (int mark : marks) {
System.out.println(mark);
}This prints:
72
85
90
This form is ideal when you:
- only need the value
- are reading, printing, summing, or counting
- are not modifying the array
- are not working with related arrays
The loop variable should describe the kind of data you are handling. For example:
for (String name : names)
for (int mark : marks)
for (double temp : temperatures)
for (char letter : letters)Note the differentiation between singular (individual array element) and plural (array of elements).
Avoid vague names like x, item, or thing.
The examples below show the enhanced for loop in the situations where it shines: clean, readable processing of each element when the index is irrelevant.
String[] players = {"Ana", "Ben", "Ming", "Hermione"};
for (String player : players) {
System.out.println(player);
}int[] scores = {98, 55, 40, 91, 30};
int count = 0;
for (int score : scores) {
if (score >= 50) {
count++;
}
}
System.out.println("Passing scores: " + count);int[] prices = {5, 3, 10, 2};
int total = 0;
for (int price : prices) {
total += price;
}
System.out.println("Total = " + total);char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (char vowel : vowels) {
System.out.println("Vowel: " + vowel);
}double[] temps = {18.5, 20.1, 22.8};
for (double temp : temps) {
System.out.println(temp + " °C");
}- Related arrays let us store connected data across multiple arrays where index alignment matters.
- Use regular index loops when you need index access or are working across multiple arrays.
- Use the enhanced for loop when you only need the values and want cleaner syntax.
Annotated solutions to this problem set can be found here.
Create two related arrays:
String[] nameswith 5 player namesint[] scoreswith 5 corresponding scores
Print each player’s name and score on the same line using a regular index-based loop.
Ana: 12
Ben: 22
Ming: 18
Lila: 31
Kai: 15
Using the same two related arrays (names[] and scores[]):
Write code to find:
- the highest score
- the name of the player who earned it
Then print the pair together.
Top player: Lila with 31 points
Ask the user for 5 temperature readings and store them in a double[] temps.
Use an enhanced for loop to compute the average.
Average temperature = 21.44 °C
Given:
int[] averages = {81, 90, 99, 78, 77, 94};Use an enhanced for loop to count how many students have made the honour roll with an average of 80 percent or higher.
Print the count.
Honour roll students: 4
Create the following related arrays:
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};Ask the user for a month abbreviation (e.g., "Apr").
Find the matching index and print how many days are in that month.
Enter a month: Apr
Apr has 30 days.
Given:
double[] prices = {5.99, 3.50, 12.00, 1.25};Use an enhanced for loop to compute the total.
Total = 22.74
Create two related arrays:
String[] firstNames = {"Amy", "Ben", "Chris", "Dana"};
String[] lastNames = {"Lee", "Wong", "Wong", "Patel"};Ask the user for a last name.
Print all first + last name pairs that match the search.
Search last name: Wong
Ben Wong
Chris Wong
Ask the user for 5 integers, store them in an array, then create a new array where the values appear in reverse order.
Use a normal index loop (not enhanced for) to build the reversed array.
Original: 3 9 1 4 7
Reversed: 7 4 1 9 3
Given:
int[] nums = {3, 10, 4, 7, 12, 5};Use an enhanced for loop to count how many numbers are even. Print the final count.
Even numbers: 3
Create two related arrays:
String[] items = {"Pen", "Notebook", "Eraser", "Pencil"};
double[] costs = {1.25, 3.50, 0.99, 0.75};Ask the user for the name of an item.
Search for it using a regular for loop and print the matching price.
Enter item: Eraser
Price: $0.99