下面的代碼演示了遍歷Collection集合的6種方法,注意Collection集合的遍歷遠不止於增強for循環,和迭代器兩種。
代碼如下:
1 package com.qls.traverse; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.Collections; 6 import java.util.Enumeration; 7 import java.util.Iterator; 8 import java.util.LinkedList; 9 import java.util.List; 10 import java.util.Stack; 11 12 /** 13 * 下面是遍歷Collection的幾種方法,以List接口為例: 14 * @author 秦林森 15 * 16 */ 17 public class ListTest { 18 19 public static void main(String[] args) { 20 // TODO Auto-generated method stub 21 String[] s="sixi is one of the most beautiful villages in china".split(" "); 22 List<String> list = Arrays.asList(s); 23 /** 24 * 第一種方法用增強for循環。(這里List之所以能用增強for循環其原因在於它實現了Iterable接口) 25 */ 26 for(String str:list){ 27 System.out.print(str+" "); 28 } 29 System.out.println(); 30 System.out.println("************"); 31 /** 32 * 第二種方法用Iterator 33 */ 34 Iterator<String> it = list.iterator(); 35 while(it.hasNext()){ 36 String next = it.next(); 37 System.out.print(next+" "); 38 } 39 System.out.println(); 40 System.out.println("************"); 41 /** 42 * 第三種方法主要針對LinkedList。因為LinkedList 既有棧(stack)的特點,又有隊列(Queue) 43 * 的特點。所以遍歷LinkedList中的元素。根據stack和queue,可以進行相關的遍歷。 44 * 遍歷的方法如下所示: 45 */ 46 //Using linkedList as a stack 47 LinkedList<String> list2=new LinkedList<>(list);//創建一個LinkeList包含list中的全部元素。 48 while(!list2.isEmpty()){ 49 System.out.print(list2.removeFirst()+" "); 50 } 51 System.out.println(); 52 System.out.println("************"); 53 /** 54 * Using linkedList as a queue 55 */ 56 LinkedList<String> list3=new LinkedList<>(list); 57 while(list3.peek() != null){ 58 System.out.print(list3.poll()+" "); 59 } 60 System.out.println(); 61 System.out.println("************"); 62 /** 63 * 第四種方法把所有的Collection都可以當做Enumeration進行遍歷 64 * Collections.enumeration(c) 65 */ 66 ArrayList<String> list4=new ArrayList<>(list); 67 Enumeration<String> e = Collections.enumeration(list4); 68 while(e.hasMoreElements()){ 69 System.out.print(e.nextElement()+" "); 70 } 71 /**第五種方法 72 * 當然還有其他方法如: 73 */ 74 System.out.println(); 75 System.out.println("************"); 76 for(int i=0;i<list4.size();i++){ 77 System.out.print(list4.get(i)+" "); 78 } 79 System.out.println(); 80 System.out.println("************"); 81 /**第六種方法: 82 *再如: 83 */ 84 while(!list4.isEmpty()){ 85 int index=0; 86 System.out.print( list4.remove(index++)+" "); 87 } 88 /** 89 * 備注:在List接口中的所有實現類中最常用的是ArrayList LinkedList 90 * ArraList比LinkedList的速度快,一般情況下選中ArrayList的情況比LinkedList多。 91 * 在ArrayList源碼中有一個serialVersionUID,這個數字保證了, 92 * 寫入文件(ObjectOutputStream.writeObject(Object)) 93 * 讀取文件(ObjectInputStream.readObject())可以順利進行, 94 * 並且指明這個數字,可以保持各個版本的兼容性。有利於文件傳輸。 95 */ 96 97 } 98 99 }/*Output: 100 sixi is one of the most beautiful villages in china 101 ************ 102 sixi is one of the most beautiful villages in china 103 ************ 104 sixi is one of the most beautiful villages in china 105 ************ 106 sixi is one of the most beautiful villages in china 107 ************ 108 sixi is one of the most beautiful villages in china 109 ************ 110 sixi is one of the most beautiful villages in china 111 ************ 112 sixi is one of the most beautiful villages in china *///:~
