Java ArrayList iterate example shows various ways to iterate through ArrayList. The example also shows how to iterate ArrayList in reverse order.
How to Iterate Java ArrayList?
There are several ways using which you can iterate through elements of Java ArrayList.
1) Using while loop
This is the simplest method to iterate through elements of an ArrayList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); int i = 0; while ( i < aListNumbers.size() ){ //get the element System.out.println(aListNumbers.get(i)); //increase the index i++; } |
Output
1 2 3 |
1 2 3 |
2) Using for loop
You can use the for loop as well.
1 2 3 |
for( int i = 0; i < aListNumbers.size(); i++){ System.out.println( aListNumbers.get(i) ); } |
Output
1 2 3 |
1 2 3 |
3) Using enhanced for loop (Java 1.5 and above version)
If you are using Java version 1.5 and above, you can use enhanced for loop.
1 2 3 |
for( Integer i : aListNumbers){ System.out.println(i); } |
Output
1 2 3 |
1 2 3 |
4) Using Iterator
ArrayList provides an Iterator object which can be used to iterate through ArrayList using its hashNext()
and next()
methods.
1 2 3 4 5 6 7 8 9 10 |
//get the Iterator object from ArrayList using iterator method Iterator<Integer> itr = aListNumbers.iterator(); /* * Use hasNext() method to check if there are any more elements * and next() method to fetch the next element from Iterator object. */ while(itr.hasNext()){ System.out.println( itr.next() ); } |
Output
1 2 3 |
1 2 3 |
5) Using ListIterator
The ListIterator can also be used instead of an Iterator. Get the ListIterator object of an ArrayList using listIterator()
method.
1 2 3 4 5 6 7 8 9 10 |
//get the ListIterator object from ArrayList using listIterator method ListIterator<Integer> listItr = aListNumbers.listIterator(); /* * Use hasNext() method to check if there are any more elements * and next() method to fetch the next element from ListIterator object. */ while(listItr.hasNext()){ System.out.println( listItr.next() ); } |
Output
1 2 3 |
1 2 3 |
The ListIterator class also provides hasPrevious()
and previous()
methods to iterate the ArrayList in the reverse order.
6) Using foreach loop (Java 8)
If you are using Java 8, you can use forEach
loop to iterate through Java ArrayList object in just one line.
1 |
aListNumbers.forEach( i -> System.out.println(i) ); |
Output
1 2 3 |
1 2 3 |
How to iterate ArrayList in reverse order?
If you want to iterate ArrayList in reverse order, you can use for loop and start with the end index (i.e. ArrayList’s size – 1 because ArrayList index starts with 0) and go up to the first index (which is 0) as given below.
1 2 3 4 5 6 7 8 9 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); for( int i = aListNumbers.size() - 1; i >= 0 ; i-- ){ System.out.println(aListNumbers.get(i)); } |
Output
1 2 3 |
3 2 1 |
Alternatively, you can reverse the ArrayList first and then iterate in the normal way to get the elements in the reverse order like given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ArrayList<Integer> aListNumbers = new ArrayList<Integer>(); aListNumbers.add(1); aListNumbers.add(2); aListNumbers.add(3); /* * Reverse ArrayList using reverse method * of Collections class. */ Collections.reverse(aListNumbers); for(Integer i : aListNumbers) System.out.println(i); |
Output
1 2 3 |
3 2 1 |
However, reverse
method approach is not suggested, because it modifies the original ArrayList. It might even create performance problems if the ArrayList is too large. Plus, if the intention is just to iterate the ArrayList in reverse order, reversing it first is overkill.
This example is a part of the ArrayList in Java tutorial.
Please let me know your views in the comments section below.
really helpful