📖 Scenario: You have a list of daily temperatures recorded in a week. You want to look at these temperatures both from the start of the week to the end and then from the end of the week back to the start.
🎯 Goal: Build a Java program that stores the temperatures in an array, then prints them forward (start to end) and backward (end to start) to understand how to traverse arrays in both directions.
📋 What You'll Learn
Create an integer array called temperatures with exactly these values: 23, 25, 22, 20, 24, 26, 21
Create an integer variable called length that stores the length of the temperatures array
Use a for loop with variable i to traverse the temperatures array forward from index 0 to length - 1
Use a for loop with variable i to traverse the temperatures array backward from index length - 1 to 0
Print the temperatures in forward order separated by spaces on one line
Print the temperatures in backward order separated by spaces on the next line
💡 Why This Matters
🌍 Real World
Looking at data in both forward and backward order helps in many real-world tasks like analyzing trends over time or reversing sequences.
💼 Career
Understanding how to traverse arrays forward and backward is a fundamental skill for software developers working with data structures and algorithms.
Progress0 / 4 steps
1
Create the temperatures array
Create an integer array called temperatures with these exact values: 23, 25, 22, 20, 24, 26, 21
DSA Java
Hint
Use curly braces {} to list the values inside the array.
2
Create the length variable
Create an integer variable called length and set it to the length of the temperatures array using temperatures.length
DSA Java
Hint
Use temperatures.length to get the number of elements in the array.
3
Traverse forward and backward
Use a for loop with variable i to traverse the temperatures array forward from index 0 to length - 1 and print each temperature followed by a space. Then use another for loop with variable i to traverse the temperatures array backward from index length - 1 to 0 and print each temperature followed by a space.
DSA Java
Hint
Use two separate for loops: one counting up from 0 to length - 1, and one counting down from length - 1 to 0.
4
Print the final output
Add a System.out.println() after the forward traversal loop to move to the next line before printing the backward traversal. This will print the temperatures forward on one line and backward on the next line.
DSA Java
Hint
Use System.out.println() with no arguments to move to the next line.
Practice
(1/5)
1. What does forward traversal mean in the context of a linked list?
easy
A. Skipping every other node while visiting
B. Visiting nodes from the tail to the head in reverse order
C. Visiting nodes from the head to the tail in order
D. Visiting nodes randomly without order
Solution
Step 1: Understand linked list structure
A linked list has nodes connected from head to tail in one direction.
Step 2: Define forward traversal
Forward traversal means visiting nodes starting at the head and moving to the tail in order.
Final Answer:
Visiting nodes from the head to the tail in order -> Option C
Quick Check:
Forward traversal = head to tail [OK]
Hint: Forward means start at head and go next nodes [OK]
Common Mistakes:
Confusing forward with backward traversal
Thinking traversal is random
Assuming skipping nodes is traversal
2. Which Java code snippet correctly traverses a singly linked list forward?