In this article, we will discuss various ways to iterate through Arrays in Java i.e.;
Various ways to iterate through Arrays:
- Standard/traditional/regular for-loop
- Enhanced for-loop or for-each loop in Java 1.5 version
- Using forEach() method in Java 1.8 Streams
- Using while loop
- Using do-while loop
- Iterating Arrays in reverse-order
- Read Arrays class in detail with example
Let us move forward and discuss all possible ways to iterate Arrays in Java
1. Standard or traditional for-loop
IteratingArraysUsingForLoop.java
package in.bench.resources.iterating.arrays.in.java;
public class IteratingArraysUsingForLoop {
public static void main(String[] args) {
// integer array
int[] intArray = {369, 358, 364, 370, 397, 311, 386};
// iterating using traditional for-loop
for(int i = 0; i < intArray.length; i++) {
// printing values
System.out.println(intArray[i]);
}
}
}
Output:
369
358
364
370
397
311
386
2. Enhanced for-loop or for-each loop in Java 1.5 version
IteratingArraysUsingEnhancedForLoop.java
package in.bench.resources.iterating.arrays.in.java;
public class IteratingArraysUsingEnhancedForLoop {
public static void main(String[] args) {
// String array
String[] strArray = {
"Vikram",
"Ajith",
"Vijay",
"Dhanush",
"Suriya"
};
// iterating using enhanced for-loop
for(String str : strArray) {
System.out.println(str);
}
}
}
Output:
Vikram
Ajith
Vijay
Dhanush
Suriya
3. Using Iterable.forEach() in Java 1.8 version
IteratingArraysUsingForEachLoopInJava8.java
package in.bench.resources.iterating.arrays.in.java;
import java.util.Arrays;
import java.util.List;
public class IteratingArraysUsingForEachLoopInJava8 {
public static void main(String[] args) {
// String array
String[] strArray = {
"Vikram",
"Ajith",
"Vijay",
"Dhanush",
"Suriya"
};
// convert Arrays into List
List<String> list = Arrays.asList(strArray);
// iterating using for-each loop in java 8
list.forEach(num -> System.out.println(num));
}
}
Output:
Vikram
Ajith
Vijay
Dhanush
Suriya
4. Using while-loop
IteratingArraysUsingWhileLoop.java
package in.bench.resources.iterating.arrays.in.java;
public class IteratingArraysUsingWhileLoop {
public static void main(String[] args) {
// count variable
int temp = 0;
// String array
String[] strArray = {
"Vikram",
"Ajith",
"Vijay",
"Dhanush",
"Suriya"
};
// iterating using while loop
while(temp < strArray.length) {
// printing values
System.out.println(strArray[temp]);
// increment count value by 1
temp++;
}
}
}
Output:
Vikram
Ajith
Vijay
Dhanush
Suriya
5. Using do-while-loop
IteratingArraysUsingDoWhileLoop.java
package in.bench.resources.iterating.arrays.in.java;
public class IteratingArraysUsingDoWhileLoop {
public static void main(String[] args) {
// count variable
int temp = 0;
// String array
String[] strArray = {
"Vikram",
"Ajith",
"Vijay",
"Dhanush",
"Suriya"
};
// iterating using do-while loop
do {
// printing values
System.out.println(strArray[temp]);
// increment count value by 1
temp++;
} while(temp < strArray.length);
}
}
Output:
Vikram
Ajith
Vijay
Dhanush
Suriya
6. Iterating Arrays in reverse order
This can be demonstrated either by using
- for loop
- while loop
- do-while loop
IteratingArraysInReverseOrder.java
package in.bench.resources.iterating.arrays.in.java;
public class IteratingArraysInReverseOrder {
public static void main(String[] args) {
// String array
String[] strArray = {
"Vikram",
"Ajith",
"Vijay",
"Dhanush",
"Suriya"
};
// iterating using traditional for-loop
for(int i = strArray.length -1; i >= 0; i--) {
// printing values
System.out.println(strArray[i]);
}
}
}
Output:
Suriya
Dhanush
Vijay
Ajith
Vikram
Hope, you found this article very helpful. If you any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.
Related Articles:
- Arrays class
- Searching element from Arrays using Binary Search Algorithm
- Arrays v/s ArrayList
- Various ways to iterate Arrays in Java – 5 ways
- Various ways to remove duplicate elements from Arrays in Java – 5 ways
- How to find duplicate in String Arrays ?
Happy Coding !!
Happy Learning !!