zoukankan      html  css  js  c++  java
  • list集合,map集合遍历

     1 import java.util.ArrayList;   
     2 import java.util.Iterator;   
     3 import java.util.List;   
     4   
     5 /**  
     6 *遍历集合List  
     7  * @author Men、叔 
     8 *  Email 844357411@qq.com  【Men 叔 Java厂】:http://jq.qq.com/?_wv=1027&k=cfqLhZ
     9  */  
    10 public class Demo {   
    11   
    12     /**  
    13      * @param args  
    14      */  
    15     public static void main(String[] args) {   
    16        
    17         List list = new ArrayList();   
    18         list.add("aaa");   
    19        list.add("bbb");   
    20         //1:利用下标   
    21      for(int i = 0 ; i< list.size();i++){   
    22                
    23             System.out.println(list.get(i));   
    24        }   
    25         //2:for- each   
    26         for(Object o:list){   
    27           System.out.println(o);   
    28         }   
    29         //3:迭代器   
    30        Iterator it = list.iterator();   
    31        while(it.hasNext()){   
    32           System.out.println(it.next());   
    33         }   
    34        //4:Object数组    
    35       Object o[];   
    36         o = list.toArray();   
    37         for(int i =  0 ; i <o.length;i++){   
    38         System.out.println(o[i]);   
    39        }   
    40        //5:toString   
    41        System.out.println(list.toString());   
    42   
    43     }   
    44   
    45 }  
    46 public class Demo{
    47 public static void main(String[] args) {
    48 
    49 
    50   Map<String, String> map = new HashMap<String, String>();
    51   map.put("1", "value1");
    52   map.put("2", "value2");54   
    55   //1:普遍使用,二次取值
    56   System.out.println("通过Map.keySet遍历key和value:");
    57   for (String key : map.keySet()) {
    58    System.out.println("key= "+ key + " and value= " + map.get(key));
    59   }
    60   
    61   //2:迭代器遍历
    62   System.out.println("通过Map.entrySet使用iterator遍历key和value:");
    63   Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
    64   while (it.hasNext()) {
    65    Map.Entry<String, String> entry = it.next();
    66    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    67   }
    68   
    69   //3:foreach 推荐,尤其是容量大时
    70   System.out.println("通过Map.entrySet遍历key和value");
    71   for (Map.Entry<String, String> entry : map.entrySet()) {
    72    System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
    73   }
    74 
    75   //4
    76   System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
    77   for (String v : map.values()) {
    78    System.out.println("value= " + v);
    79   }
    80  }
    81 }
  • 相关阅读:
    C#生成CHM文件(外篇使用hha.dll)
    prototype原理详解
    避免在代码中直接任意使用ConfigurationManager.AppSettings
    页面生命周期的来龙去脉(详细)
    nRF52832 SAADC sampling
    nRF52832开发日志--SAADC调试
    boost学习笔记(七)---date_time库
    Boost学习笔记(六) progress_display注意事项
    Boost学习笔记(五) progress_display
    扩展progress_timer的计时精度
  • 原文地址:https://www.cnblogs.com/mm0712/p/5147138.html
Copyright © 2011-2022 走看看