java语言基础--遍历

Iterator迭代器

for循环遍历,和while遍历

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import com.dss.bean.Person;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class Main {

    public static void main(String[] args) {
        forArrayList();

    }
    public  static void forArrayList() {
        final Collection c = new ArrayList();
        c.add(new Person("马云", 14));
        c.add(new Person("刘强东", 88));
        c.add(new Person("雷军", 56));
        for(Iterator i1 = c.iterator();i1.hasNext();) {
            System.out.println(i1.next());
        }
    }
    public  static void whileArrayList() {
        final Collection c = new ArrayList();
        final Iterator i = c.iterator();
        // 多态在ArrayList里面实现了Iterator的方法
        
//        System.out.println(i.next());
//        System.out.println(i.next());
//        System.out.println(i.next());
        while (i.hasNext()) {
            System.out.println(i.next());
        }
    }
}

 

posted on 2018-08-19 11:11  董大志  阅读(123)  评论(0)    收藏  举报

导航