import java.util.ArrayList; import java.util.Iterator; public class IteratorAndForEach { public static void main(String[] args) { ArrayList animals = new ArrayList(); animals.add("cat"); animals.add("elephant"); animals.add("shark"); // Display the contents of the ArrayList with an Iterator Iterator iter = animals.iterator(); while (iter.hasNext()) System.out.println(iter.next()); System.out.println(); // Display the contents of the ArrayList with a for-each loop for (String beast : animals) System.out.println(beast); } }