java遍历集合list_java集合List的遍历方式

循序渐进学习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 list=new ArrayList();

list.add("a");

list.add("b");

list.add("c");

list.add("d");

list.add("e");

//方法一:(迭代器(1))

for(Iterator iterator=list.iterator();iterator.hasNext();){

String st=iterator.next();

System.out.println("遍历方法一的list的输出的String 是"+st);

}

//方法二:for循环(1)

for(int i=0;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 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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值