ArrayList的遍历方式

本文介绍了ArrayList集合的四种遍历方式:使用Iterator迭代器、增强for循环、普通for循环及通过get方法结合索引访问。每种方式都有其适用场景,有助于理解和掌握ArrayList的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

集合ArrayList是接口List的一种子类,它的特点是:存储的元素是有序的.底层的数据结构是数组.查询快,增删慢.在众多集合中ArrayList的遍历又是比较特殊的,下面就写一下它的四中遍历方式,代码如下



public class ArrayListDemo {
	public static void main(String[] args) {
		List<String> al = new ArrayList<String>();
		al.add("wangw1");
		al.add("wangw3");
		al.add("wangw4");
		al.add("wangw5");
		
		//遍历方式1
		Iterator<String> it1 = al.iterator();
		while(it1.hasNext()){
			System.out.println(it1.next());
		}
		
		//遍历方式2
		for(Iterator it2 = al.iterator();it2.hasNext();){
			System.out.println(it2.next());
		}
		
		//遍历方式3
		for(String temp:al){
			System.out.println(temp);
		}
		
		//遍历方式4
		for(int i = 0;i<al.size();i++){
			System.out.println(al.get(i));
		}
	}
}



以上就是四种遍历方式.分别用到了for循环和Iterator遍历器.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值