import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class MainClass { public static void main(String[] args) { List cats = new ArrayList(); for (int i = 0; i < 7; i++) cats.add(new Cat(i)); Iterator e = cats.iterator(); while (e.hasNext()) ((Cat) e.next()).id(); } } class Cat { private int catNumber; public Cat(int i) { catNumber = i; } public void id() { System.out.println("Cat #" + catNumber); } } /* */
Cat #0 Cat #1 Cat #2 Cat #3 Cat #4 Cat #5 Cat #6