java中遍曆List的幾種方法

來源:互聯網
上載者:User

1.使用索引(index)來遍曆List

2.使用Iterator來遍曆List。(for-each其實也是利用Iterator)

注意:如果僅僅是遍曆,那麼可以可以使用索引或Iterator來遍曆。但是,如果你在遍曆的同時還要刪除元素,則最好使用Iterator來遍曆和刪除元素,這樣可以避免ConcurrentModificationException異常。

下面是使用for、for-each和Iterator來遍曆

執行個體代碼:


import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
 *
 * Java program to demonstrate different ways to loop, iterate or traverse List in Java.
 * There are three example in this program, first examples shows how to loop List
 * using Iterator, Second Example shows Looping over List using advanced Java 5 for loop
 * while third and last examples demonstrate use of traditional for loop for traversing
 * a List in Java.
 *
 * @author Javin Paul
 */
public class ListLoopExample{
  
    public static void main(String args[]){
      
        //First example to iterate List in Java using Iterator
        List<String> languages = Arrays.asList("Java", "C++", "Scala", "Groovy");
 
 
        //Getting Iterator from List in Java
        Iterator<String> iterator = languages.iterator();
        System.out.println("Iterating List in Java using Iterator ");
 
 
        //Iterating through all elements of List
        while (iterator.hasNext()) {
            System.out.printf("Current element in List is %s %n", iterator.next());
        }
 
 
        //Second example of Iterating over List in Java using foreach loop
        System.out.println("Looping List in Java using foreach loop");
        for (String city : languages) {
            System.out.println("List Element: " +  city);
        }
      
        //Third example of Looping List using traditional for loop
        for(int i =0; i<languages.size(); i++){
            System.out.printf("programming language #%d in List is : %s %n", i, languages.get(i) );
        }
      
    
    }
}
 
Output:
Iterating List in Java using Iterator
Current element in List is London
Current element in List is Tokyo
Current element in List is NewYork
Current element in List is Mumbai
Looping List in Java using foreach loop
List Element: London
List Element: Tokyo
List Element: NewYork
List Element: Mumbai
City #0 in List is : London
City #1 in List is : Tokyo
City #2 in List is : NewYork
City #3 in List is : Mumbai

再看例子

package com.hisense.tiger.list;

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

public class ListTest {
 public static void main(String[] args)
 {
  List<String> list = new ArrayList<String>();
  long t1,t2;
  for(int j = 0; j < 10000000; j++)
  {
   list.add("aaaaaa" + j);
  }
  System.out.println("List first visit method:");
  t1=System.currentTimeMillis();
  for(String tmp:list)
  {
   //System.out.println(tmp);
  }
  t2=System.currentTimeMillis();
  System.out.println("Run Time:" + (t2 -t1) + "(ms)");
  System.out.println("List second visit method:");
  
  t1=System.currentTimeMillis();
  for(int i = 0; i < list.size(); i++)
  {
   list.get(i);
   //System.out.println(list.get(i));
  }
  t2=System.currentTimeMillis();
  System.out.println("Run Time:" + (t2 -t1) + "(ms)");
  
  System.out.println("List Third visit method:");
  Iterator<String> iter = list.iterator();
  
  t1=System.currentTimeMillis();
  while(iter.hasNext())
  {
   iter.next();
   //System.out.println(iter.next());
  }
  t2=System.currentTimeMillis();
  System.out.println("Run Time:" + (t2 -t1) + "(ms)");
    
  System.out.println("Finished!!!!!!!!");
  
  
 }
}

 測試結果如下:

List first visit method:
Run Time:170(ms)
List second visit method:
Run Time:10(ms)
List Third visit method:
Run Time:34(ms)
Finished!!!!!!!!

    測試的結論很奇怪,第一種方法是java語言支援的新文法,代碼最簡潔,但是在三種方法中,效能確是最差的,取size進行遍曆效能是最高的,求牛人解釋

相關文章

聯繫我們

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