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
  1. import java.util.ArrayList;
  2.  
  3. public class ArrayListTraditionalForLoop {
  4.     public static void main(String[] args) {
  5.     ArrayList<Integer> list = new ArrayList<Integer>();
  6.       list.add(14);
  7.       list.add(7);
  8.       list.add(21);
  9.       list.add(28);
  10.      System.out.println("Traditional For Loop");
  11.       for (int index = 0; index < list.size(); index++) {            
  12.           System.out.println(list.get(index));     
  13.       }  
  14.     }
  15. }
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
  1. import java.util.ArrayList;
  2.  
  3. public class ArrayListEnhancedForLoop {
  4.     public static void main(String[] args) {
  5.     ArrayList<Integer> list = new ArrayList<Integer>();
  6.       list.add(14);
  7.       list.add(7);
  8.       list.add(21);
  9.       list.add(28);
  10.      System.out.println("Enhanced For Loop");
  11.       for (Integer element : list) {             
  12.            System.out.println(element);        
  13.       }
  14.     }
  15. }
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
  1. import java.util.ArrayList;
  2.  
  3. public class ArrayListWhileLoop {
  4.     public static void main(String[] args) {
  5.     ArrayList<Integer> list = new ArrayList<Integer>();
  6.       list.add(14);
  7.       list.add(7);
  8.       list.add(21);
  9.       list.add(28);
  10.       System.out.println("While Loop");        
  11.       int count = 0;       
  12.       while ( count < list.size()) {
  13.      System.out.println(list.get(count));
  14.          count++;
  15.       }
  16.     }
  17. }
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
  1. import java.util.*;
  2.  
  3. public class ArrayListIterator {
  4.     public static void main(String[] args) {
  5.     ArrayList<Integer> list = new ArrayList<Integer>();
  6.       list.add(14);
  7.       list.add(7);
  8.       list.add(21);
  9.       list.add(28);
  10.       System.out.println("While with iterator");
  11.       Iterator itr = list.iterator();
  12.       while (itr.hasNext()) {
  13.          System.out.println(itr.next());
  14.       }
  15.     }
  16. }
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

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on