ArrayList与LinkedList时间复杂度之对比

package ArrayList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

public class Compare {
public static final int N=50000;
public static List values;
static{
    Integer vals[]=new Integer[N];
    Random r=new Random();
   
    for(int i=0,currval=0;i<N;i++){
        vals[i]=currval;
        currval+=r.nextInt(100)+1;
     }       
    values=Arrays.asList(vals);
    }
static long timeList(List lst){
    long start=System.currentTimeMillis();
    for(int i=0;i<N;i++){
        int index=Collections.binarySearch(lst, values.get(i));
        if(index!=i)
            System.out.println("ERRORRRRRRRRRRRRRR");
    }
    return System.currentTimeMillis()-start;
}

public static void main(String args[]){
    System.out.println("Arraylist's Time used:"+timeList(new ArrayList(values)));
    System.out.println("LinkedList's TIme used:"+timeList(new LinkedList(values)));
    for(int i=0;i<N;i++){
        System.out.println(values.get(i));
    }
}
}

你可能感兴趣的:(LinkedList)