------<a href="http://www.itheima.com" target="blank">Java培训、Android培训</a>、期待与您交流! -------
使用Iterator 遍历器对List集合进行遍历
public static void main(String[] args) {
// 1、创建多个狗狗对象
Dog ououDog = new Dog("欧欧", "雪娜瑞");
Dog yayaDog = new Dog("亚亚", "拉布拉多");
Dog meimeiDog = new Dog("美美", "雪娜瑞");
Dog feifeiDog = new Dog("菲菲", "拉布拉多");
// 2、创建ArrayList集合对象并把多个狗狗对象放入其中
List dogs = new ArrayList();
dogs.add(ououDog);
dogs.add(yayaDog);
dogs.add(meimeiDog);
dogs.add(2, feifeiDog); //添加的时候,指定位置
// 3、通过迭代器依次输出集合中所有狗狗的信息
System.out.println("使用Iterator遍历,所有狗狗的昵称和品种分别是:");
Iterator it = dogs.iterator(); //调用集合的iterator方法
while (it.hasNext()) { //循环遍历
Dog dog = (Dog) it.next(); //遍历出来的对象默认是 object 类型,需要转换
System.out.println(dog.getName()+"\t"+dog.getStrain());
}
}