import java.util.*;
public class IteratorTest {
public static void main(String[] args) {
ArrayList cats = new ArrayList();
// create cats
for(int i = 0; i < 7; i++)
cats.add(new Cat(i));
// print all the cats
Iterator e = cats.iterator();
while(e.hasNext()) {
System.out.println((Cat)e.next());
}
}
}
class Cat {
private int catNumber;
public Cat(int i) {
catNumber = i;
}
public String toString() {
return "Cat #" + catNumber;
}
}
IteratorTest.java output
Cat #0
Cat #1
Cat #2
Cat #3
Cat #4
Cat #5
Cat #6
Tag: Study Code Program Java
No comments:
Post a Comment