zoukankan      html  css  js  c++  java
  • list和map的遍历

     

      

    list和map是java集合中的必备类,当然他们的遍历也是至关重要的。

    list的遍历:

    1.for循环

    for (Integer each : list) {
        System.out.println(each);
    }
    

    2.iterator循环

    Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }    
    

    3.正规的for循环

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i));
    }

    整个demo如下:

    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(2);
    list.add(3);
    System.out.println("第一种方式:精简的for循环---------------");
    for (Integer each : list) {
    	System.out.println(each);
    }
    System.out.println();
    System.out.println("第二种方式:iterator指针---------------");
    Iterator<Integer> iterator = list.iterator();
    while (iterator.hasNext()) {
    	System.out.println(iterator.next());
    }
    System.out.println();
    System.out.println("第三种方式:正常的for循环---------------");
    for (int i = 0; i < list.size(); i++) {
    	System.out.println(list.get(i));
    }
    System.out.println();
    

    map的遍历:

    使用entrySet()方法遍历:

    for (Entry<String, String> each : map.entrySet()) {
        System.out.println(each.getKey() + ":" + each.getValue());
    } 

     demo如下:

    Map<String, String> map = new HashMap<String, String>();
    map.put("1", "小王");
    map.put("2", "小李");
    map.put("3", "小黑");
    for (Entry<String, String> each : map.entrySet()) {
    	System.out.println(each.getKey() + ":" + each.getValue());
    }
    

     

    Ride the wave as long as it will take you.
  • 相关阅读:
    jQuery学习(三)
    HTML基础
    对于跨域问题的解决
    Spring boot 默认静态资源路径与手动配置访问路径
    json:java中前台向后台传对象数据
    javascrit常用互动方法
    java IO流
    HTML中data* 属性
    java中一些对象(po,vo,dao,pojo)等的解释
    使用mybatis generator插件,自动生成dao、dto、mapper等文件
  • 原文地址:https://www.cnblogs.com/jianpanaq/p/9073475.html
Copyright © 2011-2022 走看看