Using the For-Each Loop with Collections: ArrayList : For Each Loop « Statement Control « Java Tutorial






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








4.7.For Each Loop
4.7.1.The For-Each Version of the for Loop
4.7.2.The for-each loop is essentially read-only
4.7.3.The for each loop for an enum data type
4.7.4.Using the For-Each Loop with Collections: ArrayList
4.7.5.Use a for-each style for loop
4.7.6.Using 'for each' to loop through array
4.7.7.Iterating over Multidimensional Arrays: Use for-each style for on a two-dimensional array
4.7.8.Using break with a for-each-style for