Mega Code Archive

 
Categories / Java Tutorial / Statement Control
 

Using the For-Each Loop with Collections

For-Each Loop can be used to any object that implements the Iterable interface. This includes all collections defined by the Collections Framework, import java.util.ArrayList;   public class MainClass {         public static void main(String args[]) {      ArrayList<Double> list = new ArrayList<Double>();     list.add(10.14);     list.add(20.22);     list.add(30.78);     list.add(40.46);     double sum = 0.0;     for(double itr : list)       sum = sum + itr;     System.out.println(sum);      }  } 101.6