【轉】Java中 List的遍曆

來源:互聯網
上載者:User

標籤:

原文網址:http://blog.csdn.net/player26/article/details/3955906

 

  1. import java.util.ArrayList;  
  2. import java.util.Iterator;  
  3. import java.util.List;  
  4. public class ListTest {  
  5.  public static void main(String[] args) {  
  6.   List<Integer> list = new ArrayList<Integer>();  
  7.   list.add(1);  
  8.   list.add(2);  
  9.   list.add(3);  
  10.   for (Iterator i = list.iterator(); i.hasNext();)  
  11.    System.out.println(i.next()); // line 1  
  12.  }  
  13. }  
  14.    
  15. public class ListTest2 {  
  16.   public static void main(String[] args){  
  17.          List list = new ArrayList();  
  18.                  list.add(new Integer(100));  
  19.                  list.add(new Float(150.60));  
  20.                  list.add(new String("abc"));  
  21.          for(Iterator i = list.iterator(); i.hasNext();)  
  22.                  System.out.println(i.next());  
  23.    
  24. //   List<Integer> list = new ArrayList<Integer>();  
  25. //         list.add(1);  
  26. //         list.add(2);  
  27. //         list.add(3);  
  28. //         for(Integer i : list){  
  29. //         System.out.println(i);        // OK  
  30. //         }  
  31.        }  
  32. }  
  33.    
  34. //雖然List的Generic是Integer,但.next()返回的類型是Object  
  35. public class ListTest3 {  
  36.  public static void main(String[] args) {  
  37.   // List<Integer> list = new ArrayList<Integer>();  
  38.   // list.add(1);  
  39.   // list.add(2);  
  40.   // list.add(3);  
  41.   // for(Iterator i = list.iterator(); i.hasNext();){  
  42.   // Integer integerRef = (Integer) i.next(); //line 1  
  43.   // //Compile Error  
  44.   // System.out.println(integerRef); // line 2  
  45.   // }  
  46.   List<Integer> list = new ArrayList<Integer>();  
  47.   list.add(1);  
  48.   list.add(2);  
  49.   list.add(3);  
  50.   for (Iterator<Integer> i = list.iterator(); i.hasNext();) {  
  51.    Integer integerRef = i.next(); // line 1  
  52.    // OK  
  53.    System.out.println(integerRef); // line 2  
  54.   }  
  55.  }  
  56. }   

 

對List的遍曆有三種方式   
    
   List<A>    list    =    new    ArrayList<A>();   
   list.add(new    A());   
   list.add(new    A());   
   ...   
    
   第一種:   
   for(Iterator<A>    it    =    list.iterator();    it.hasNext();    )    {   
       ....   
   }   
   這種方式在迴圈

執行過程中會進行資料鎖定,    效能稍差,    同時,如果你想在迴圈過程中去掉某個元素,只能調用it.remove方法,    不能使用list.remove方法,    否則一定出並發訪問的錯誤.   
    
   第二種:   
   for(A    a    :    list)    {   
       .....   
   }   
   內部調用第一種,    換湯不換藥,    這種迴圈方式還有其他限制,    不建議使用它   
    
   第三種:   
   for(int    i=0;    i<list.size();    i++)    {   
       A    a    =    list.get(i);   
       ...   
   }   
   內部不鎖定,    效率最高,    但是當寫多線程時要考慮並行作業的問題! 

【轉】Java中 List的遍曆

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.