A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© papercup 中级黑马   /  2014-4-10 10:03  /  1023 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

遍历集合的方式有哪些?求大神讲解下, 多谢!

5 个回复

倒序浏览
三种方式吧,for,增强for和迭代器三种遍历方式
  1.         public class ForTraverse{
  2.                 public static void main(String[] args){
  3.                         Collection<Integer> collection =  new ArrayList<Integer>();
  4.                         for(int i=0;i<5;i++){
  5.                                 collection.add(i);
  6.                         }
  7.                         System.out.println("[");
  8.                        
  9.                         //普通for遍历
  10.                         for(int i=0;i<collection.size();i++){
  11.                                 System.out.print(i+",");
  12.                         }
  13.                         System.out.println("]");
  14.                 }
  15.         }




  16.         public class ForeachTraverse{
  17.                 public static void main(String[] args){
  18.                         Collection<Integer> collection =  new ArrayList<Integer>();
  19.                         for(int i=0;i<5;i++){
  20.                                 collection.add(i);
  21.                         }
  22.                         System.out.println("]");
  23.                        
  24.                         //增强for遍历
  25.                         for(Integer i:collection){
  26.                                 System.out.print(i+",");
  27.                         }       
  28.                         System.out.println("]");
  29.                 }
  30.         }
  31.        


  32.         public class IteratorTraverse{
  33.                 public static void main(String[] args){
  34.                         Collection<Integer> collection =  new ArrayList<Integer>();
  35.                         for(int i=0;i<5;i++){
  36.                                 collection.add(i);
  37.                         }
  38.                         System.out.println("[");
  39.                        
  40.                         //iterator遍历
  41.                         Iterator<Integer> it=collection.iterator();
  42.                                
  43.                         while(it.hasNext()){
  44.                                 System.out.println(collection);
  45.                         }
  46.                        
  47.                         System.out.println("]");
  48.                 }
  49.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报
有三种方法:
比如 List list = new ArrayList();
list.add...

1. 对于ArrayList来说速度比较快, 用for循环, 以size为条件遍历:
int size = list.size();
for(int i = 0; i < size; i ++) {
  list.get(i);
}

2. 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代:
Iterator it = list.iterator();
while(it.hasNext()) {
  Object obj = it.next();
}

3. JDK较新版本中有的方法, 但是我不太明白它的原理, 和js中的遍历很像:
for(Object obj : list) {
  // obj 就是一次取出来的元素.
}
回复 使用道具 举报
本帖最后由 東少 于 2014-4-10 14:53 编辑

对集合的遍历优先选用迭代器的方式 方式有四种
1,普通for循环
for(int i=0;i<list.size( );i++){
      System.out.println(list.get(i));
}
2,增加for循环
for(Object obj:list){
    System.out.println(obj);
}3,for循环迭代器
for(Iterator it=list.iterator();it.hasNext( ); ){
    System.out.println(it.next( ));
}
4, while循环迭代器
Iterator it=list.iterator( );
while(it.hasNext( )){
    System.out.println(it.next( ));
}
回复 使用道具 举报
1. 对于ArrayList来说速度比较快, 用for循环, 以size为条件遍历:
int size = list.size();
for(int i = 0; i < size; i ++) {
  list.get(i);
}

2. 集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代:
Iterator it = list.iterator();
while(it.hasNext()) {
  Object obj = it.next();
}

3. JDK较新版本中有的方法, 但是我不太明白它的原理, 和js中的遍历很像:
for(Object obj : list) {
  // obj 就是一次取出来的元素.
}
回复 使用道具 举报
同意楼上所说的三种方式
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马