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進行遍曆效能是最高的,求牛人解釋