ArrayList集合遍历的八种方式

ArrayList集合遍历的七种方式

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

public class Ergodic {
	public static void main(String[] args) {
		
		System.out.println("ArrayList集合的遍历");
		List list = new ArrayList();
		list.add(1);
		list.add(2);

		
		System.out.println("1.for循环:");
		for(int i = 0; i<list.size(); i++) {
			System.out.println(list.get(i));
		}

		
		System.out.println("2.foreach循环:");
		for(Object l : list) {
			System.out.println(l);
		}

		
                System.out.println("3.链表变为数组内容后遍历:");
                Integer[] inteArray = new Integer[list.size()];
                list.toArray(inteArray);
                for(int inte : inteArray) {
                    System.out.println(inte);
                }


		System.out.println("4.while迭代器遍历:");
		Iterator iterator1 = list.iterator();
		while(iterator1.hasNext()) {// hasNext()方法:判断该指针所指位置是否有元素
			System.out.println(iterator1.next());// next()方法:1.获取该指针指向的元素 2.并把指针指向下一个元素(即指针后移一位)
		}


		System.out.println("5.for迭代器遍历:");
		//该处将while改为for循环遍历,可以当遍历完后便释放迭代器对象,更节省内存
		for(Iterator iterator3 = list.iterator(); iterator3.hasNext() ;) {
			System.out.println(iterator3.next());
		}

		
		System.out.println("6.1 ListIterator迭代器向后遍历:");
		ListIterator iterator2 = list.listIterator();
		while(iterator2.hasNext()) {// 将list集合先从头向后遍历,指针指向最后
			System.out.println(iterator2.next());
		}


		System.out.println("6.2 ListIterator迭代器向前遍历");
		while(iterator2.hasPrevious()) {// 将list集合从末尾先前遍历,指针指到第一个元素
			System.out.println(iterator2.previous());
		}		
	}
}

 

对于集合的遍历,推荐下面代码,节省内存.

for(Iterator iterator3 = list.iterator(); iterator3.hasNext() ;) {
            System.out.println(iterator3.next());
        }

还有对于集合的foreach遍历集合,其实就是反编译后就是上面的代码.若想节省内存还是推荐上面代码. 

Java8可以使用Stream API来遍历集合

// List集合遍历的第八种方式
list.stream().forEach(System.out::println);

 

集合反编译代码

// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Ergodic.java

import java.io.PrintStream;
import java.util.*;

public class Ergodic
{

	public Ergodic()
	{
	}

	public static void main(String args[])
	{
		
		System.out.println("ArrayList集合的遍历");
		List list = new ArrayList();
		list.add(Integer.valueOf(1));
		list.add(Integer.valueOf(2));


		System.out.println("1.for循环:");
		for (int i = 0; i < list.size(); i++)
			System.out.println(list.get(i));

		System.out.println("2.foreach循环:");
		Object l;
		for (Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(l))
			l = iterator.next();

		System.out.println("3.链表变为数组内容后遍历:");
		Integer inteArray[] = new Integer[list.size()];
		list.toArray(inteArray);
		Integer ainteger[];
		int j1 = (ainteger = inteArray).length;
		for (int i1 = 0; i1 < j1; i1++)
		{
			int inte = ainteger[i1].intValue();
			System.out.println(inte);
		}

		System.out.println("4.while迭代器遍历:");
		for (Iterator iterator1 = list.iterator(); iterator1.hasNext(); System.out.println(iterator1.next()));

		System.out.println("5.for迭代器遍历:");
		for (Iterator iterator3 = list.iterator(); iterator3.hasNext(); System.out.println(iterator3.next()));

		System.out.println("6.1 ListIterator迭代器向后遍历:");
		ListIterator iterator2;
		for (iterator2 = list.listIterator(); iterator2.hasNext(); System.out.println(iterator2.next()));

		System.out.println("6.2 ListIterator迭代器向前遍历");
		for (; iterator2.hasPrevious(); System.out.println(iterator2.previous()));
	}
}

 

数组

                System.out.println("数组的遍历");
		int[] a = new int[] {1,2};


		System.out.println("1.for循环:");
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}


		System.out.println("2.foreach循环:");
		for(int b : a) {
			System.out.println(b);
		}

		
		System.out.println("3.Arrays工具类:");
		System.out.println(Arrays.toString(a));
		

数组反编译代码

                System.out.println("数组的遍历");
		int a[] = {
			1, 2
		};
		System.out.println("1.for循环:");
		for (int i = 0; i < a.length; i++)
			System.out.println(a[i]);

		System.out.println("2.foreach循环:");
		int ai[];
		int k = (ai = a).length;
		for (int j = 0; j < k; j++)
		{
			int b = ai[j];
			System.out.println(b);
		}

		System.out.println("3.Arrays工具类:");
		System.out.println(Arrays.toString(a));

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值