首页 > 其他 > 详细

iterator的使用

时间:2016-12-04 23:10:29      阅读:223      评论:0      收藏:0      [点我收藏+]

package ListPackage;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Demo4 {
//**************使用迭代器遍历map和list**************
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Map<String,Integer> map = new HashMap<>();//图
        List<Integer> list = new LinkedList<Integer>();//链表
        
        map.put("Mike", 15);
        map.put("John", 4);
        map.put("Amy", 45);
        map.put("Michael", 8);
        map.put("Marry", 67);
        map.put("Zack", 39);
        
        list.add(34);
        list.add(56);
        list.add(54);
        list.add(89);
        list.add(308);
        
//        使用迭代器遍历map
        Iterator<Map.Entry<String,Integer>> it = map.entrySet().iterator();
        while(it.hasNext()){
            Map.Entry<String, Integer> entries = it.next();
            System.out.println(entries.getKey()+" "+entries.getValue());
        }
        System.out.println();
        
//        使用迭代器遍历list
        Iterator<Integer> it1 = list.iterator();
        while(it1.hasNext()){
            System.out.println(it1.next());
        }

    }

}

iterator的使用

原文:http://www.cnblogs.com/ihins/p/6132203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!