动物类:
package DemoLearnTest.迭代器03;
public class Animal {
private String site;
private String name;
public Animal(){
}
public Animal(String site, String name){
this.site = site;
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public void setSite(String site) {
this.site = site;
}
public String getSite() {
return site;
}
public String getName() {
return name;
}
}
main函数:
package DemoLearnTest.迭代器03;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test {
public static void main(String[] args) {
Collection animal = new ArrayList();
Animal aaa = new Animal();
Animal bbb = new Animal();
Animal ccc = new Animal();
aaa.setSite("食肉动物");
aaa.setName("老虎");
bbb.setSite("杂食动物");
bbb.setName("猪八戒");
ccc.setSite("食草动物");
ccc.setName("喜羊羊");
animal.add(aaa);
animal.add(bbb);
animal.add(ccc);
Iterator<Animal> it = animal.iterator();
while (it.hasNext()){
Animal an001 = it.next();
System.out.println(an001.getSite() + "-" + an001.getName());
}
}
}
运行结果如下所示: