Five Ways to Loop Through An Array in Java

This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.

It’s been a little while since I wrote something Java-related (March 28, 2019 was the last time, to be exact) so I figured I write something simple.

Hence Five Ways to Loop Through An Array in Java.

An array is one of the most basic data structures in programming. It’s essentially a fixed-length list of similar items (referred to as elements) that are often accessed via their index. The index is simply their position in the array, starting with position zero.

One of the benefits of an array is the ability to loop through each element and process some sort of work on the element.

Looping through an array is often called iterating through or iterating over the array.

Since I’m a Java developer, I present to the world Five Ways to Loop Through An Array in Java.

For the purpose of this exercise, I’ll loop through the following String array:

String[] strings = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};

And, because I actually want to do some work, each iteration of the loop will call the following method:

private void doTheThing(final String string) {

While loop

Using a while loop to iterate over an array isn’t the cleanest way to do so, but it is possible:

int i = 0;

while(wi < strings.length) {

For Loop

for(int i = 0; i < strings.length; i++) {

Enhanced For Loop

The Enhanced For Loop (sometimes also called a foreach loop) was introduced in Java 5. With the enhanced for loop, you no longer have to keep track of the index (the int i in the previous two examples). This improves readability.

The Enhanced For Loop’s strength is also its weakness. With no access to the index, you can’t peak at the next item (e.g. strings[i+1]) or look at the previous item (e.g. strings[i-1]). Thankfully this isn’t always needed.

for(final String string : strings) {


Java 8 introduced functional programming aided by lamdas and streams.

Arrays by themselves are not streams, but they can be easily used by streams by calling;

You can also use Stream.of, but it immediately calls


Here’s the Java code for Stream.of, showing that it just calls

     * Returns a sequential ordered stream whose elements are the specified values.
     * @param <T> the type of stream elements
     * @param values the elements of the new stream
     * @return the new stream
    @SuppressWarnings("varargs") // Creating a stream from an array is safe
    public static<T> Stream<T> of(T... values) {


For fun, here’s a recursive way to loop through arrays. Don’t do this except as an intellectual exercise:

private void loop(final String[] strings, final int i) {
    if(i < strings.length) {
        loop(strings, i + 1);

Start the recursive loop by calling the method with the array and the number zero:

loop(strings, 0);

The End

Leave a Reply

Note that comments won't appear until approved.