Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Java ArrayList foreach

This small Java example shows how to traverse Java ArrayList using foreach loop.
  1. import java.util.ArrayList;
  2.  
  3. //Java ArrayList foreach
  4. public class ArrayListForEachExample {
  5.  
  6.     public static void main(String args[]){
  7.        
  8.         //create new ArrayList
  9.         ArrayList<String> aListColors = new ArrayList<String>();
  10.         aListColors.add("Red");
  11.         aListColors.add("Green");
  12.         aListColors.add("Blue");
  13.        
  14.         //traverse ArrayList using foreach loop
  15.        
  16.         //read it as for each color in colors ArrayList
  17.         for(String color: aListColors){
  18.             System.out.println(color);
  19.         }
  20.     }
  21. }
  22.  
  23. /*
  24. Output of above Java ArrayList foreach example would be
  25. Red
  26. Green
  27. Blue
  28. */
Your rating: None Average: 2 (1 vote)
Share this
Anonymous's picture

Thanks

Thanks
Anonymous's picture

nice....

nice....

Post new comment

3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.