// Erika Wolfe, CSE 143 // Class that shows off different ways to iterate over a list import java.util.*; public class IteratingExample { public static void main(String[] args) { List tas = new ArrayList(); tas.add("Shanti"); tas.add("Melissa"); tas.add("Josh"); tas.add("Jason"); tas.add("Suzanne"); tas.add("Isaac"); tas.add("BEN"); System.out.println(tas); for (int i = 0; i < tas.size(); i++) { System.out.println(tas.get(i)); } System.out.println(); for (String ta : tas) { System.out.println(ta); } System.out.println(); // The for each loop is readable and convenient, and // provides a way to iterate over a non-indexed structure // (like a Set). Modification is not allowed within the // structure - a ConcurrentModificationException will be // thrown. Another way to iterate is to use an Iterator, // which allows for modification. (See SetExample). Iterator itr = tas.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } itr = tas.iterator(); } }