There are 5 ways you can iterate through an ArrayList
1. Using For Loop
2. Using Advanced For Loop
3. Using While loop
4. Using Iterator
5. Using Stream API (Java 8)
Read Also: How to find the length of an ArrayList in java
Let's write all the ways through which one can iterate or traverse or loop over ArrayList in java.
Program for How to loop ArrayList in Java :
1. Iterate ArrayList using For loop
We can iterate or loop over ArrayList using for loop as shown below in the example:
import java.util.*;
public class ArrayListLoopExample
{
public static void main (String[] args)
{
ArrayList<Integer> al = new ArrayList();
al.add(13);
al.add(7);
al.add(36);
al.add(89);
al.add(97);
/*For Loop Example for traversing ArrayList*/
System.out.println("Using For Loop");
for (int counter = 0; counter < al.size(); counter++) {
System.out.println(al.get(counter));
}
}
}
Output:
Using For Loop
13
7
36
89
97
2. Iterate ArrayList using Advanced for loop
We can iterate or loop over ArrayList using an advanced for loop as shown below in the example:
import java.util.*;
public class ArrayListLoopExample2
{
public static void main (String[] args)
{
ArrayList<Integer> al2 = new ArrayList();
al2.add(131);
al2.add(71);
al2.add(361);
al2.add(891);
al2.add(971);
/* Advanced For Loop Example for iterating ArrayList*/
System.out.println("Using Advanced For Loop");
for (Integer num : al2) {
System.out.println(num);
}
}
}
Output:
Using Advanced For Loop
131
71
361
891
971
3. Iterate ArrayList using the While loop
We can iterate or loop over ArrayList using the while loop as shown below in the example:
import java.util.*;
public class ArrayListLoopExample3
{
public static void main (String[] args)
{
ArrayList<Integer> al3 = new ArrayList();
al3.add(133);
al3.add(73);
al3.add(363);
al3.add(893);
al3.add(973);
/* While Loop Example for iterating ArrayList*/
System.out.println("Using While Loop");
int count = 0;
while (al3.size() > count) {
System.out.println(al3.get(count));
count++;
}
}
}
Output:
Using While Loop
133
73
363
893
973
4. Iterate ArrayList using Iterator
We can iterate or loop over ArrayList using the iterator as shown below in the example:
import java.util.*;
public class ArrayListLoopExample4
{
public static void main (String[] args)
{
ArrayList<Integer> al4 = new ArrayList();
al4.add(134);
al4.add(734);
al4.add(3634);
al4.add(8934);
al4.add(9734);
/* Iterator Example for Traversing ArrayList */
System.out.println("Using Iterator");
Iterator it = al4.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
Output:
Using Iterator
134
734
3634
8934
9734
5. Iterate ArrayList using Stream API (Java 8)
We can iterate or loop over ArrayList using Stream API as shown below in the example:
import java.util.*;
public class ArrayListLoopExample5
{
public static void main (String[] args)
{
ArrayList<Integer> al5 = new ArrayList();
al5.add(1345);
al5.add(7345);
al5.add(36345);
al5.add(89345);
al5.add(97345);
/* Collection stream() util example */
System.out.println("Using Stream API");
al5.forEach((num) -> {
System.out.println(num);
});
}
}
Output:
Using Stream API
1345
7345
36345
89345
97345
Please mention in the comments if you have any questions regarding how to loop ArrayList in java.