码迷,mamicode.com
首页 > 编程语言 > 详细

java集合List的遍历方式

时间:2015-12-11 16:34:35      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

   循序渐进学习java 集合的遍历方式:

  一、先以list集合为例:

  package com.test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class testing {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        List<String> list=new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        
        //方法一:(迭代器(1))
        for(Iterator<String> iterator=list.iterator();iterator.hasNext();){
            String st=iterator.next();
            
            System.out.println("遍历方法一的list的输出的String 是"+st);
        }
        
        //方法二:for循环(1)
        for(int i=0;i<list.size();i++){
            String st=list.get(i);
            System.out.println("遍历方法二的list的输出的String ="+st);
        }
        
        
        //方法三:for循环(2)
        for(String st:list){
            System.out.println("遍历方法三的list输出的String 为 "+st);
        }
        
        //方法四 :(迭代器(2))
        Iterator<String> iterator=list.iterator();
        while(iterator.hasNext()){
            String st=iterator.next();
            System.out.println("遍历方法四list输出的String is "+st);
            
        }
    }

}

 

 

输出如下:

遍历方法一的list的输出的String 是a
遍历方法一的list的输出的String 是b
遍历方法一的list的输出的String 是c
遍历方法一的list的输出的String 是d
遍历方法一的list的输出的String 是e
遍历方法二的list的输出的String =a
遍历方法二的list的输出的String =b
遍历方法二的list的输出的String =c
遍历方法二的list的输出的String =d
遍历方法二的list的输出的String =e
遍历方法三的list输出的String 为 a
遍历方法三的list输出的String 为 b
遍历方法三的list输出的String 为 c
遍历方法三的list输出的String 为 d
遍历方法三的list输出的String 为 e
遍历方法四list输出的String is a
遍历方法四list输出的String is b
遍历方法四list输出的String is c
遍历方法四list输出的String is d
遍历方法四list输出的String is e

 

!============记录点滴成长============!

 

java集合List的遍历方式

标签:

原文地址:http://www.cnblogs.com/dark-passion/p/5039404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!