By Tony Bevis on Wednesday, 11 October 2023
Category: Java

Java programming course: 6.3 Looping over arrays

In the previous lesson we defined a class that utilises an array.

Looping over arrays

Java allows four different mechanisms for looping over the elements of an array. Looping is sometimes referred to as iteration.

The standard "for" loop

The standard for loop is in the following format:

Note the following:


Example: To iterate over the animals in the pen, first retrieve the array of animals:

Now you can iterate over them with the following code:

Note the following:


The above loop is only iterating the specific number of times corresponding to the actual number of animals in the pen (making use of the getCount() method), rather than iterating over all ten elements of the array. When you need to iterate over every array element then all arrays have a publicly available length attribute (not method) which will provide that number. Change the above for statement as indicated:

The above will iterate over all ten array elements, but be aware that you only added two animals to the pen so after the first two elements the remaining eight elements will all be null. To ignore these, you would need an extra conditional check, as follows:

A better approach would be for the getAnimals() method to only return the array items that aren't null, so modify it as follows:

The Arrays class is in package java.util which you will therefore need to import.

The copyOfRange() static method of Arrays returns a new array based on a subset of a given array, using the first and last index range specified in arguments two and three. The check for null is no longer required in the loop:

​ The "for-each" loop

The "for-each" loop is a simplified version of the standard for loop and follows this format:

Note the following:


You could use the "for-each" looping mechanism to iterate over the animalsInZoo array as in the following method:

In the above, the variable anAnimal will contain each array element's Animal object reference in turn.

Although the syntax of the "for-each" mechanism is simpler than the standard for loop, the main disadvantage is that you do not now have the element index number during each iteration.

The "while" loop

The while looping mechanism repeats a block of code while a boolean condition is true, and follows this format: 

Applied to the animalsInPen array you could iterate as follows:  

Note the following:

The "do-while" loop

The "do-while" loop is similar to the "while" loop except that the conditional check is performed at the end of the block. It follows this format:

Applied to the animalsInPen array you could do this:

The primary difference compared to the normal "while" loop is that "do-while" will always execute the code inside the block at least once, since the conditional check only occurs at the end of the block. You therefore need to reinstate the check for null, since if no animals at all are in the pen you need to prevent the access to the first element.

In the next lesson we will show how arrays can be sorted into a sequence.

Next lesson: 6.4 Sorting arrays 

Related Posts

Leave Comments