ArrayList /Vector/LinkedList用/迭代器遍历 /列表迭代器遍历 /使用size()和get()方法遍历/使用增强for遍历集合

package org.westos.demo3;

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

public class Test {

	public static void main(String[] args) {
		ArrayList list = new ArrayList<>();
		list.add("张大大");
		list.add("张老二");
		list.add("张老三");
		list.add("张老四");
		list.add("张老五");
		printArrList1(list);
		printArrList2(list);
		printArrList3(list);
		printArrList4(list);
	}
	//使用增强for循环
	private static void printArrList4(ArrayList list) {
		for (String a : list) {
			System.out.println(a);
		}

	}

	// 使用size()和get()方法遍历
	private static void printArrList3(ArrayList list) {
		for (int i = 0; i < list.size(); i++) {
			list.get(i);
			System.out.println(list.get(i));
		}

	}

	// 使用列表迭代器遍历
	private static void printArrList2(ArrayList list) {
		ListIterator iterator = list.listIterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
	}

	// 迭代器遍历
	private static void printArrList1(ArrayList list) {
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

	}

}
package org.westos.demo3;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class Test2 {

	public static void main(String[] args) {
		Vector vector = new Vector<>();
		vector.add(1);
		vector.add(2);
		vector.add(3);
		vector.add(4);
		vector.add(5);
		printVector(vector);
		printVector2(vector);
		printVector3(vector);
		printVector4(vector);
	}

	// 使用增强for遍历
	private static void printVector4(Vector vector) {
		for (Integer a : vector) {
			System.out.println(a);
		}

	}

	// 使用size()和get()方法遍历
	private static void printVector3(Vector vector) {
		for (int i = 0; i < vector.size(); i++) {
			System.out.println(vector.get(i));
		}

	}

	// 使用Vector特有的迭代器遍历
	private static void printVector2(Vector vector) {
		Enumeration elements = vector.elements();
		while (elements.hasMoreElements()) {
			System.out.println(elements.nextElement());
		}

	}

	// 使用迭代器遍历
	private static void printVector(Vector vector) {
		Iterator iterator = vector.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

	}

}
package org.westos.demo3;

import java.io.ObjectInputStream.GetField;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Vector;

public class Test3 {

	public static void main(String[] args) {
		LinkedList list = new LinkedList<>();
		list.add(11);
		list.add(22);
		list.add(33);
		list.add(44);
		list.add(55);
		printlinkedlist1(list);
		printlinkedlist2(list);
		printlinkedlist3(list);
		printlinkedlist4(list);
	}

	// d)使用增强for遍历
	private static void printlinkedlist4(LinkedList list) {
		for (Integer a : list) {
			System.out.println(a);
		}

	}

	// c)使用size()和get()方法遍历
	private static void printlinkedlist3(LinkedList list) {
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

	}

	// b)使用列表迭代器遍历
	private static void printlinkedlist2(LinkedList list) {
		ListIterator iterator = list.listIterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

	}

	// a)使用迭代器遍历
	private static void printlinkedlist1(LinkedList list) {
		Iterator iterator = list.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}

	}

}



你可能感兴趣的:(ArrayList /Vector/LinkedList用/迭代器遍历 /列表迭代器遍历 /使用size()和get()方法遍历/使用增强for遍历集合)