首页 > 编程语言 > 详细

JAVA整理07---使用迭代器对容器进行遍历

时间:2019-06-20 10:47:43      阅读:103      评论:0      收藏:0      [点我收藏+]

容器遍历之迭代器的使用

一个简单的main函数demo来记录下:

public static void main(String args[]){
        ArrayList<Integer>list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
       for(Iterator<Integer> it=list.iterator();it.hasNext();){
           int tmp=it.next();
           System.out.println(tmp);
       }

        HashMap<String,String>map=new HashMap<>();
        map.put("语文","90");
        map.put("数学","80");
        map.put("英语","100");
        Set<String>keys=map.keySet();
        for(Iterator<String>it=keys.iterator();it.hasNext();){
            String tmp=it.next();
            System.out.println("key is "+tmp);
            System.out.println("value is "+map.get(tmp));
        }
          Set<Map.Entry<String,String>> enter=map.entrySet();
        for(Iterator it=enter.iterator();it.hasNext();){
            System.out.println(it.next());
        }
    }

Collection对容器数据进行一些排序,洗牌等等。。

JAVA整理07---使用迭代器对容器进行遍历

原文:https://www.cnblogs.com/CszShuzi/p/11056845.html

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