Arraylist with Looping
In this article, we will see how to loop arraylist in java
Its very much common requirement to iterate or loop through ArrayList in java applications
There are mainly 4 ways to loop through ArrayList in java
1) Traditional For loop
2) Enhanced For loop
3) While loop
4) Iterator
Let’s see each of these ways with an example
1) Traditional For loop
- import java.util.ArrayList;
- public class ArrayListTraditionalForLoop {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("Traditional For Loop");
- for (int index = 0; index < list.size(); index++) {
- System.out.println(list.get(index));
- }
- }
- }
import java.util.ArrayList; public class ArrayListTraditionalForLoop { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("Traditional For Loop"); for (int index = 0; index < list.size(); index++) { System.out.println(list.get(index)); } } }
Note :
In the above example, we have used Traditional for-loop to iterate the arraylist
2) Enhanced For loop
- import java.util.ArrayList;
- public class ArrayListEnhancedForLoop {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("Enhanced For Loop");
- for (Integer element : list) {
- System.out.println(element);
- }
- }
- }
import java.util.ArrayList; public class ArrayListEnhancedForLoop { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("Enhanced For Loop"); for (Integer element : list) { System.out.println(element); } } }
Note :
In the above example, we have used advanced/enhanced for loop to iterate arraylist
3) While loop
- import java.util.ArrayList;
- public class ArrayListWhileLoop {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("While Loop");
- int count = 0;
- while ( count < list.size()) {
- System.out.println(list.get(count));
- count++;
- }
- }
- }
import java.util.ArrayList; public class ArrayListWhileLoop { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("While Loop"); int count = 0; while ( count < list.size()) { System.out.println(list.get(count)); count++; } } }
Note :
In the above example, we have used while loop to iterate arraylist
4) Iterator
- import java.util.*;
- public class ArrayListIterator {
- public static void main(String[] args) {
- ArrayList<Integer> list = new ArrayList<Integer>();
- list.add(14);
- list.add(7);
- list.add(21);
- list.add(28);
- System.out.println("While with iterator");
- Iterator itr = list.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- }
- }
import java.util.*; public class ArrayListIterator { public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(14); list.add(7); list.add(21); list.add(28); System.out.println("While with iterator"); Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } } }
Note :
In the above example, we have used iterator with while loop to iterate arraylist
In most of the cases, we use enhanced for loop to iterate over arraylist
In some cases, it is mandatory to use iterator especially when we are running with concurrent modification issue