0% found this document useful (0 votes)
6 views3 pages

Traversing An Array in Java

The document explains various methods for traversing an array in Java, including using for loops, while loops, enhanced for loops (foreach), and the Arrays.toString() method. Each method is accompanied by example code demonstrating how to access and print array elements. A summary table is also provided, outlining the characteristics of each traversal method.

Uploaded by

joashodchas09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Traversing An Array in Java

The document explains various methods for traversing an array in Java, including using for loops, while loops, enhanced for loops (foreach), and the Arrays.toString() method. Each method is accompanied by example code demonstrating how to access and print array elements. A summary table is also provided, outlining the characteristics of each traversal method.

Uploaded by

joashodchas09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Traversing an Array in Java

Traversing an array means accessing each element of the array one by one. This is typically
done using loops such as for, while, and foreach loops. Traversing is essential for performing
operations like searching, modifying, or displaying array elements.

1. Using a for Loop to Traverse an Array


A for loop is one of the most common ways to iterate through an array using an index-based
approach.

Example: Traversing an Array Using a for Loop


public class ForLoopTraversal {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

// Using a for loop to traverse the array


for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}

Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50

2. Using a while Loop to Traverse an Array


A while loop can also be used when the number of iterations is not explicitly known or when
using external conditions.

Example: Traversing an Array Using a while Loop


public class WhileLoopTraversal {
public static void main(String[] args) {
int[] numbers = {5, 15, 25, 35, 45};

int i = 0;
while (i < numbers.length) {
System.out.println("Element at index " + i + ": " + numbers[i]);
i++;
}
}
}

Output:
Element at index 0: 5
Element at index 1: 15
Element at index 2: 25
Element at index 3: 35
Element at index 4: 45

3. Using an Enhanced for Loop (Foreach) to Traverse an


Array
The enhanced for loop (also called the foreach loop) simplifies array traversal by automatically
iterating through all elements without using an index.

Example: Traversing an Array Using an Enhanced for Loop


public class ForeachTraversal {
public static void main(String[] args) {
int[] numbers = {100, 200, 300, 400, 500};

for (int num : numbers) {


System.out.println("Element: " + num);
}
}
}

Output:
Element: 100
Element: 200
Element: 300
Element: 400
Element: 500

4. Printing Elements of an Array Using Arrays.toString()


Instead of using loops, you can use Arrays.toString() to print an entire array in a single line.

Example:
import java.util.Arrays;
public class PrintArray {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};

System.out.println(Arrays.toString(numbers));
}
}

Output:
[10, 20, 30, 40, 50]

Summary of Traversing Methods


Traversing Method Description Example Code
Uses an index to
For Loop for (int i = 0; i < arr.length; i++)
traverse elements
Iterates based on a
While Loop while (i < arr.length) {}
condition
Enhanced For Loop Iterates without for (int num : arr) {}
(Foreach) using an index
Prints array as a
Arrays.toString() System.out.println(Arrays.toString(arr));
string

Would you like additional variations or examples? 🚀

You might also like