0
0
Javaprogramming~15 mins

Array traversal in Java

Choose your learning style8 modes available
menu_bookIntroduction

Array traversal means looking at each item in an array one by one. It helps us check or use every element inside the array.

When you want to print all items in a list of names.
When you need to find the biggest number in a list of scores.
When you want to add up all values in a list of prices.
When you want to check if a certain value exists in a list.
When you want to change or update each item in a list.
regular_expressionSyntax
Java
public class ArrayTraversal {
    public static void main(String[] args) {
        int[] numbers = {10, 20, 30, 40, 50};
        for (int index = 0; index < numbers.length; index++) {
            int currentElement = numbers[index];
            // Use currentElement here
        }
    }
}

The for loop starts at index 0 because arrays in Java start at 0.

numbers.length gives the total number of elements in the array.

emoji_objectsExamples
line_end_arrow_notchThis shows what happens if the array is empty. The loop does not run at all.
Java
int[] emptyArray = {};
for (int index = 0; index < emptyArray.length; index++) {
    System.out.println(emptyArray[index]);
}
line_end_arrow_notchThis shows traversal when there is only one element in the array.
Java
int[] singleElementArray = {99};
for (int index = 0; index < singleElementArray.length; index++) {
    System.out.println(singleElementArray[index]);
}
line_end_arrow_notchThis example shows how to identify the first, last, and middle elements during traversal.
Java
int[] numbers = {5, 10, 15, 20};
for (int index = 0; index < numbers.length; index++) {
    if (index == 0) {
        System.out.println("First element: " + numbers[index]);
    } else if (index == numbers.length - 1) {
        System.out.println("Last element: " + numbers[index]);
    } else {
        System.out.println("Middle element: " + numbers[index]);
    }
}
code_blocksSample Program

This program shows how to look at each score in an array, print it, add 5 points to each, and then print the updated scores.

Java
public class ArrayTraversal {
    public static void main(String[] args) {
        int[] scores = {85, 90, 78, 92, 88};
        System.out.println("Scores before traversal:");
        for (int index = 0; index < scores.length; index++) {
            System.out.println("Score at index " + index + ": " + scores[index]);
        }
        System.out.println("\nAdding 5 bonus points to each score...");
        for (int index = 0; index < scores.length; index++) {
            scores[index] = scores[index] + 5;
        }
        System.out.println("\nScores after adding bonus points:");
        for (int index = 0; index < scores.length; index++) {
            System.out.println("Score at index " + index + ": " + scores[index]);
        }
    }
}
OutputSuccess
emoji_objectsImportant Notes
line_end_arrow_notch

Time complexity is O(n) because we visit each element once.

line_end_arrow_notch

Space complexity is O(1) since we only use a few extra variables.

line_end_arrow_notch

A common mistake is going beyond the last index by using <= instead of < in the loop condition.

line_end_arrow_notch

Use array traversal when you need to process or check every element. For searching a single item, consider stopping early.

list_alt_checkSummary

Array traversal means visiting each element one by one using a loop.

Start from index 0 and go up to length - 1.

It helps to read, update, or check all items in the array.