java 四种遍历List的方法及比较
java 四种遍历List的方法及比较
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListTest {
public static void main(String args[]){
List<Long> lists = new ArrayList<Long>();
for(Long i=0l;i<1000000l;i++){
lists.add(i);
}
Long oneOk = oneMethod(lists);
Long twoOk = twoMethod(lists);
Long threeOk = threeMethod(lists);
Long fourOk = fourMethod(lists);
System.out.println("One:" + oneOk);
System.out.println("Two:" + twoOk);
System.out.println("Three:" + threeOk);
System.out.println("four:" + fourOk);
}
public static Long oneMethod(List<Long> lists){
Long timeStart = System.currentTimeMillis();
for(int i=0;i<lists.size();i++) {
System.out.println(lists.get(i));
}
Long timeStop = System.currentTimeMillis();
return timeStop -timeStart ;
}
public static Long twoMethod(List<Long> lists){
Long timeStart = System.currentTimeMillis();
for(Long string : lists) {
System.out.println(string);
}
Long timeStop = System.currentTimeMillis();
return timeStop -timeStart ;
}
public static Long threeMethod(List<Long> lists){
Long timeStart = System.currentTimeMillis();
Iterator<Long> it = lists.iterator();
while (it.hasNext())
{
System.out.println(it.next());
}
Long timeStop = System.currentTimeMillis();
return timeStop -timeStart ;
}
public static Long fourMethod(List<Long> lists){
Long timeStart = System.currentTimeMillis();
for(Iterator<Long> i = lists.iterator(); i.hasNext();)
相关文档:
/************Student.java begin***************/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Student {
private String name;
private String password;
public String getName() {
return name;
}
public ......
JDK1.4中
Map map = new HashMap();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
}
JDK1.5中,应用新特性For-Each循环
Map m = new HashMap();
......
example: 求5的阶乘。。
如下:
public class Test {
static int multiply(int n){
if(n==1||n==0)
return n;
else
return n*multiply(n-1);
}
public static void main(String[] args){
System.out.println(multiply(10));
}
......
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BubbleSort {
private static int a[] = new int[12];
private static BufferedReader in = new BufferedReader(
new InputStreamReader(System.in));
public static void bubbleSort(int a[], i ......