0
0
Javaprogramming~15 mins

Array traversal in Java - Mini Project: Build & Apply

Choose your learning style8 modes available
folder_codeArray traversal
📖 Scenario: You are working on a simple program to process a list of daily temperatures recorded in a week. You want to look at each temperature one by one to understand the weather pattern.
🎯 Goal: Build a Java program that stores temperatures in an array and then uses a loop to visit each temperature and print it out.
📋 What You'll Learn
Create an array of integers with exactly these values: 23, 25, 22, 20, 24, 26, 21
Create a variable to hold the length of the array
Use a for loop with an index variable i to traverse the array
Print each temperature value inside the loop
💡 Why This Matters
🌍 Real World
Traversing arrays is a basic skill used in many programs that handle lists of data, like weather apps, shopping lists, or scores.
💼 Career
Understanding how to loop through arrays is essential for any programming job, as it helps process collections of information efficiently.
Progress0 / 4 steps
1
Create the temperature array
Create an integer array called temperatures with these exact values: 23, 25, 22, 20, 24, 26, 21.
Java
💡 Need a hint?

Use curly braces {} to list the values inside the array.

2
Create a variable for array length
Create an integer variable called length and set it to the length of the temperatures array using temperatures.length.
Java
💡 Need a hint?

The length property gives the number of elements in the array.

3
Traverse the array with a for loop
Use a for loop with an integer index variable i starting at 0 and running while i < length. Inside the loop, access the current temperature with temperatures[i].
Java
💡 Need a hint?

Use for (int i = 0; i < length; i++) to loop through the array indexes.

4
Print each temperature
Inside the for loop, use System.out.println(temperatures[i]) to print each temperature value.
Java
💡 Need a hint?

Use System.out.println(temperatures[i]) to print each value on its own line.