πŸ”„ Array Traversal in Java – A Complete Beginner’s Guide (With Examples)

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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *