集合遍历

  • ArraList 
List<String> arrayList = new ArrayList<String>();
          arrayList .add("xx1");
          arrayList .add("xx2");
  
          //方法1
          Iterator it1 = arrayList .iterator();
          while(it1.hasNext()){
              System.out.println(it1.next());
          }
 
         //方法2
         for(Iterator it2 = arrayList .iterator();it2.hasNext();){
              System.out.println(it2.next());
         }
 
         //方法3
         for(String tmp:arrayList ){
             System.out.println(tmp);
         }
 
         //方法4
         for(int i = 0;i < arrayList .size(); i ++){
             System.out.println(arrayList .get(i));
         }

 

  • Map

 Map<String, List<String>> map = new HashMap<String, List<String>>();
     if (map != null) {        
      for (Map.Entry<String, List<String>> entry : map.entrySet()) {
       entry.getKey(); // map
       entry.getValue();
} }

 

posted @ 2022-02-14 15:59  菜鸟笔记_-  阅读(49)  评论(0)    收藏  举报