package com.soar.collection;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import com.soar.bean.Student;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Demo5_Iterator {
/*
* * A:迭代器概述
* 集合是用来存储元素,存储的元素需要查看,那么就需要迭代(遍历)
* B:案例演示
* 迭代器的使用
*/
public static void main(String[] args) {
//demo1_Iterator();
Collection c = new ArrayList();
c.add(new Student("张三",23)); //Object obj = new Student("张三",23);
c.add(new Student("李四",24));
c.add(new Student("王五",25));
c.add(new Student("赵六",26));
//获取迭代器
Iterator it = c.iterator();
while(it.hasNext()){
//System.out.println(it.next()); //toString方法
Student s = (Student)it.next(); //向下转型
System.out.println(s.getName()+"..."+s.getAge());
}
}
private static void demo1_Iterator() {
Collection c = new ArrayList();
c.add("a");
c.add("b");
c.add("c");
c.add("d");
//对集合中的元素进行迭代(遍历)
Iterator it = c.iterator(); //获取迭代器
/* boolean b1 = it.hasNext(); //判断集合中是否有元素,有就返回true
Object obj1 = it.next();
System.out.println(b1); //true
System.out.println(obj1); //a
boolean b2 = it.hasNext(); //判断集合中是否有元素,有就返回true
Object obj2 = it.next();
System.out.println(b2); //true
System.out.println(obj2); //b
*/
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Java_基础—集合的遍历之迭代器遍历
最新推荐文章于 2024-07-01 09:10:13 发布