"

乐虎体育官网拥有全球最顶尖的原生APP,每天为您提供千场精彩体育赛事,乐虎体育官网更有真人、彩票、电子老虎机、真人电子竞技游戏等多种娱乐方式选择,乐虎体育官网让您尽享娱乐、赛事投注等,且无后顾之忧!

<cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<cite id="jnxbr"><video id="jnxbr"></video></cite><cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<var id="jnxbr"><strike id="jnxbr"></strike></var>
<cite id="jnxbr"><span id="jnxbr"><menuitem id="jnxbr"></menuitem></span></cite>
<cite id="jnxbr"></cite>
<cite id="jnxbr"><span id="jnxbr"></span></cite>
<cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<var id="jnxbr"></var>
<cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<cite id="jnxbr"></cite>
<var id="jnxbr"><strike id="jnxbr"><thead id="jnxbr"></thead></strike></var>
<cite id="jnxbr"><video id="jnxbr"><thead id="jnxbr"></thead></video></cite>
"

ArrayList ΪʲôҪʵ RandomAccess ӿڣ

С: 24Ķ 2020.12.08

ǵĿУList ӿǼÿ춼 ArrayList LinkedListϸĵͬѧûз֣ArrayList ʵ RandomAccess ӿڣ LinkedList ȴûʵ RandomAccess ӿڣΪʲôأ

public class ArrayList<E> extends AbstractList<E>
    implements List<E>, RandomAccess, Cloneable, java.io.Serializable
public class LinkedList<E> extends AbstractSequentialList<E>    
    implements List<E>, Deque<E>, Cloneable, java.io.Serializable

RandomAccess ӿǿյģRandomAccess ӿʲôأ

public interface RandomAccess {}
RandomAccess ӿ

RandomAccess һǽӿڣٷֻҪ List ʵӿڣֿ֧ʡʲôأ˵

Collections Ǽϵһ࣬ǿһ Collections ԴеĶ

public static <T> int binarySearch
    (List<? extends Comparable<? super T>> list, T key) {
    if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
        return Collections.indexedBinarySearch(list, key);
    else
        return Collections.iteratorBinarySearch(list, key);
}

ԴпԿж list Ƿ RandomAccess ʵ** ǣִ indexedBinarySearch ǣִ iteratorBinarySearch ** һ

public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) {
    if (list instanceof RandomAccess || list.size() < BINARYSEARCH_THRESHOLD) {
          return Collections.indexedBinarySearch(list, key);
    } else {
          return Collections.iteratorBinarySearch(list, key);
    }
}
private static <T> int indexedBinarySearch
    (List<? extends Comparable<? super T>> list, T key) {
    int low = 0;
    int high = list.size()-1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        Comparable<? super T> midVal = list.get(mid);
        int cmp = midVal.compareTo(key);

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1);  // key not found
}

private static <T> int iteratorBinarySearch
    (List<? extends Comparable<? super T>> list, T key){
    int low = 0;
    int high = list.size()-1;
    ListIterator<? extends Comparable<? super T>> i = list.listIterator();

    while (low <= high) {
        int mid = (low + high) >>> 1;
        Comparable<? super T> midVal = get(i, mid);
        int cmp = midVal.compareTo(key);

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1);  // key not found
}

Դʾʵ RandomAccess ӿڵ List ʹδʵ RandomAccess ӿڵ List ʹõôΪʲôҪôأȻ漰ıôһ ArrayList LinkedList ԪصΣ

public class CollectionTest {
    public static void main(String[] args){
        long arrayListIndexedTime = arrayListIndexed();
        long arrayListIteratorTime = arrayListIterator();
        long linkedListIndexedTime = linkedListIndexed();
        long linkedListIteratorTime = linkedListIterator();
        System.out.println("ArrayListͨforʱ䣺" + arrayListIndexedTime);
        System.out.println("ArrayListͨiteratorʱ䣺" + arrayListIteratorTime);
        System.out.println("LinkedListͨforʱ䣺" + linkedListIndexedTime);
        System.out.println("LinkedListͨiteratorʱ䣺" + linkedListIteratorTime);
    }

    //ArrayListͨforʱ
    public static long arrayListIndexed() {
        List<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            arrayList.add(i);
        }
        //¼ʼʱ
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < arrayList.size(); i++) {
            arrayList.get(i);
        }
        //¼ʱ
        long endTime = System.currentTimeMillis();
        //ʱ
        long resultTime = endTime - startTime;
        return resultTime;
    }

    //ArrayListͨiteratorʱ
    public static long arrayListIterator() {
        List<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            arrayList.add(i);
        }
        //¼ʼʱ
        long startTime = System.currentTimeMillis();
        Iterator<Integer> iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
        //¼ʱ
        long endTime = System.currentTimeMillis();
        //ʱ
        long resultTime = endTime - startTime;
        return resultTime;
    }

    //LinkedListͨforʱ
    public static long linkedListIndexed() {
        List<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 10000; i++) {
            linkedList.add(i);
        }
        //¼ʼʱ
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < linkedList.size(); i++) {
            linkedList.get(i);
        }
        //¼ʱ
        long endTime = System.currentTimeMillis();
        //ʱ
        long resultTime = endTime - startTime;
        return resultTime;
    }

    //LinkedListͨiteratorʱ
    public static long linkedListIterator() {
        List<Integer> linkedList = new LinkedList<>();
        for (int i = 0; i < 10000; i++) {
            linkedList.add(i);
        }
        //¼ʼʱ
        long startTime = System.currentTimeMillis();
        Iterator<Integer> iterator = linkedList.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
        //¼ʱ
        long endTime = System.currentTimeMillis();
        //ʱ
        long resultTime = endTime - startTime;
        return resultTime;
    }
}

Խ

ArrayListͨforʱ䣺1
ArrayListͨiteratorʱ䣺2
LinkedListͨforʱ䣺47
LinkedListͨiteratorʱ䣺1

һ²ԽArrayList ͨ for ͨ iterator ҪԿ죬LinkedList ͨ iterator ͨ for Ҫ졣

˵ǵӦУҪʹ List ӿڵʵ࣬ԸøЧʵʳͨʵ RandomAccess ӿ List ʵࡣ

ܽһ仰ʵ RandomAccess ӿڵ List ͨ for ѭݱʹ iterator ݸЧδʵ RandomAccess ӿڵ List ͨ iterator ݱʹ for ѭݸЧ

ǩ
ֻ