How to iterate over Array in Java?Six (6) ways to loop over an Array in Java

Iteration is a technique used to sequence through a block of code perpetually until a concrete condition either subsist or no longer subsists. Iterations are a very prevalent approach utilized with loops.
In this article, we will show you Six (6) ways to loop over an Array in Java.

1. Using Simple For loop 
2. Using Enhanced For loop 
3. Using While loop
4. Using Do - while loop
5. Using Stream.of() and forEach
6. Using Arrays.stream() and forEach

Example 1: Using Simple For loop  

// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// Simple For loop
for (int i = 0; i < users.length; i++) {

System.out.println(users[i]);
}



Example 2:  Using Enhanced For loop 

The enhanced for loop is introduced since J2SE 5.0. It is used to traverse the array or collection elements. 
// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// Enhanced For loop
for (String temp : users) {
System.out.println(temp);
}


Example 3: Using While loop

// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// while loop
int i = 0;
while (i < users.length) {
System.out.println(users[i]);
i++;
}


Example 4:  Using Do - while loop

// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// do - while loop
int j = 0;
do {
System.out.println(users[j]);
j++;
} while (j < users.length);


Example 5:  Using Stream.of() and forEach 

The Stream of(T… values) returns a sequential ordered stream whose elements are the specified values.Java Stream forEach() method is utilized to iterate over all the elements of the given Stream and to perform a Consumer action on each element of the Stream.
// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// Using Stream.of() and forEach
// print the elements of stream
Stream.of(users).forEach(System.out::println);



Example 6:  Using Arrays.stream() and forEach

The Arrays.stream() returns a sequential Stream with the elements of the array, passed as a parameter, as its source. Java Stream forEach() method is utilized to iterate over all the elements of the given Stream and to perform a Consumer action on each element of the Stream.
// create an array
String[] users = { "alpha", "beta", "giga", "gama", "tesla" };

// Using Arrays.stream() and forEach
// print the elements of stream
Arrays.stream(users).forEach(System.out::println);

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024