遍曆Collection集合中的6種方法:

來源:互聯網
上載者:User

標籤:div   隊列   oid   put   auto   span   remove   實現   ble   

下面的代碼示範了遍曆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 *///:~

 

遍曆Collection集合中的6種方法:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.