Examples -- Iterating

See full Java OOP example here....

We'll add and use this helper method for the examples:
public String listToString(List list) {
	return Arrays.toString(list.toArray());
}


Printing Each Object in List

List<String> colors = new ArrayList<String>(Arrays.asList("Yellow", "Cyan", "Magenta", "Green")); for (String eachColor: colors) { println(eachColor); }



Sending Message to Each Object in List

List<Rectangle> rectangles = Rectangle.sampleList(); //Print println("Before: " + listToString(rectangles)); for (Rectangle eachRec: rectangles) { eachRec.tripleSize(); } println("After: " + listToString(rectangles));



Summing (Accumulating)

//Sum all widths //Note: if needed add "getWidth" getter to rectangle for this sample List<Rectangle> rectangles = Rectangle.sampleList(); println("Rectangles: " + listToString(rectangles)); int sum = 0; for (Rectangle eachRec: rectangles) { sum += eachRec.getWidth(); } println("Sum of All Widths: " + sum);