list的三种遍历方法

1.最简单的for循环遍历


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

2.最方便的foreach循环遍历,在程序中用到的最多

   for(String tmp:list){};

   我经常使用这个方法遍历实体,例如:

   list是查询数据的返回值

    for(实体名 p:list){
                    Map<String,Object> map = new HashMap<String,Object>();
                    map.put("id", p.getId());
                    map.put("s1",p.getS1());
                    map.put("s2",p.getS2());
                    map.put("s3",p.getS3());
                    map.put("s4",p.getS4());
                    map.put("s5",p.getS5());
                    map.put("s6",p.getS6());
                    _list.add(map);//定义一个空的_list集合,然后put进去
                }

3.用iterator进行遍历

Iterator<String> iter = list.iterator();

while(iter.hasNext()){    
        iter.next(); 
        }

原文地址:https://www.cnblogs.com/zcleilei/p/5853192.html