Arrays are one of the most important data structures in programming. Before performing advanced operations like searching, sorting, insertion, or deletion, we must first understand how to access every element of an array.
This process is known as Array Traversal.
In this blog, we’ll learn:
- β What is Array Traversal?
- β Why do we need Traversal?
- β How Traversal works
- β Java program for Array Traversal
- β Step-by-step explanation
- β Different ways to traverse an array
- β Time Complexity of Traversal
Let’s get started!
π What is Array Traversal?
Array Traversal is the process of visiting every element of an array exactly once.
While traversing, we can:
- Display elements
- Calculate the sum
- Find the largest or smallest element
- Count occurrences
- Update values
- Perform any required operation on each element
Traversal simply means moving through the array from the first element to the last element.
π Example
Suppose we have the following array.
Index : 0 1 2 3 4
Array : 10 20 30 40 50
During traversal, the program visits the elements in this order:
10
β
20
β
30
β
40
β
50
Every element is visited exactly one time.
π€ Why Do We Need Array Traversal?
Imagine a teacher has marks of five students stored in an array.
85 90 78 92 88
Suppose the teacher wants to:
- Display all marks
- Calculate the total marks
- Find the highest mark
- Count students scoring above 80
Can this be done by looking at only one element?
No.
The program must visit every element one by one.
This process is called Traversal.
π¦ How Does Traversal Work?
The most common way to traverse an array is by using a for loop.
The loop starts from the first index (0) and continues until the last index (length – 1).
i = 0
β
i = 1
β
i = 2
β
i = 3
β
i = 4
At each iteration, the program accesses
arr[i]
π» Java Program for Array Traversal
public class ArrayTraversal {
public static void main(String[] args) {
int arr[] = {10, 20, 30, 40, 50};
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output
10
20
30
40
50
π Step-by-Step Execution
Suppose the array is
10 20 30 40 50
The loop begins with
i = 0
The program prints
arr[0]
Output
10
Next,
i = 1
The program prints
arr[1]
Output
20
Next,
i = 2
Output
30
Next,
i = 3
Output
40
Next,
i = 4
Output
50
Finally,
i = 5
Now the loop condition becomes
i < arr.length
Since
5 < 5
is false, the loop stops.
Traversal is complete.
π Visual Representation
Array
+----+----+----+----+----+
|10 |20 |30 |40 |50 |
+----+----+----+----+----+
β
Visit
β
Visit
β
Visit
β
Visit
β
Visit
The pointer moves from the first element to the last element.
π― Using Traversal to Calculate Sum
Traversal is not only used for displaying elements.
We can also calculate the sum.
public class ArraySum {
public static void main(String[] args) {
int arr[] = {10, 20, 30, 40, 50};
int sum = 0;
for(int i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("Sum = " + sum);
}
}
Output
Sum = 150
Here, every element is visited once and added to the variable sum.
π― Using Traversal to Find the Largest Element
Traversal can also be used to find the maximum value.
public class LargestElement {
public static void main(String[] args) {
int arr[] = {10, 45, 18, 92, 35};
int max = arr[0];
for(int i = 1; i < arr.length; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
System.out.println("Largest Element = " + max);
}
}
Output
Largest Element = 92
Again, every element is visited exactly once.
π Enhanced for Loop (For-Each Loop)
Java also provides another way to traverse arrays.
This is called the Enhanced for Loop or For-Each Loop.
public class EnhancedTraversal {
public static void main(String[] args) {
int arr[] = {10, 20, 30, 40, 50};
for(int value : arr) {
System.out.println(value);
}
}
}
Output
10
20
30
40
50
π€ Which Loop Should You Use?
Normal for Loop
for(int i = 0; i < arr.length; i++)
β Gives access to the index.
Useful when:
- Updating elements
- Insertion
- Deletion
- Searching by index
Enhanced for Loop
for(int value : arr)
β Simpler and easier to read.
Useful when:
- Only reading values
- Displaying elements
- Calculating sum
- Finding maximum or minimum
However, it does not provide the index directly.
β±οΈ Time Complexity of Array Traversal
During traversal, every element is visited exactly once.
Suppose the array contains n elements.
The loop executes n times.
Therefore,
Time Complexity = O(n)
π€ Why is the Time Complexity O(n)?
Consider the following array.
10 20 30 40 50
To print every element, the program must visit
- 10
- 20
- 30
- 40
- 50
If the array has 100 elements, the loop executes 100 times.
If the array has 10,000 elements, the loop executes 10,000 times.
The running time increases linearly with the number of elements.
Hence,
Traversal = O(n)
π‘ Real-Life Analogy
Imagine a teacher wants to check the attendance of students in a classroom.
Student 1
β
Student 2
β
Student 3
β
Student 4
β
Student 5
The teacher cannot skip students.
Every student must be checked one by one.
This is exactly how array traversal works.
Every element is visited exactly once.
π Key Takeaways
- Array Traversal means visiting every element of an array exactly once.
- Traversal is the foundation for many array operations.
- A for loop is the most commonly used technique for traversal.
- Java also provides an Enhanced for Loop for simpler traversal when indexes are not required.
- Traversal is used to display elements, calculate sums, find maximum and minimum values, count occurrences, and perform many other operations.
- Since every element is visited once, the time complexity of traversal is O(n).
π― Final Thoughts
Array traversal is the first and most fundamental operation you’ll perform on arrays. Almost every algorithm that works with arraysβwhether it’s searching, sorting, finding the maximum element, calculating averages, or updating valuesβbegins with traversal.
Once you understand traversal, you’ll find it much easier to learn more advanced topics such as Linear Search, Binary Search, Sorting Algorithms, and other essential concepts in Data Structures and Algorithms.
Mastering traversal is the first step toward becoming confident in solving array-based programming problems.