Jdk 接口類RandomAccess瞭解

1. 接口說明java

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.算法

列表(List)實現使用的標記接口,用於指示它們支持快速(一般是恆定時間)隨機訪問。該接口的主要目的是容許通用(通常的)算法改變其行爲,以便在應用於隨機或順序訪問列表時提供良好的性能。app

The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance.dom

操縱隨機訪問的列表最佳的算法(如ArrayList)當應用在順序訪問列表會產生二次行爲(如LinkedList)。鼓勵(支持)通用(通常的)列表算法在應用算法以前檢查給定的列表是不是該接口的實例,若是將算法應用於順序訪問列表,則該算法將提供較差的性能,並在必要時改變他們的行爲,以保證能夠接受的性能。ide

It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class,oop

能夠認識到,隨機訪問和順序訪問之間的區別一般是模糊的。例如,某些 列表實現提供漸進的線性訪問時間,若是它們得到極大的訪問時間,但其實是恆定的訪問時間。這樣的 列表實現一般應該實現此接口。根據經驗,若是對於類的典型實例,列表實現應該實現這個接口,性能

this loop:測試

這個循環this

    for (int i=0, n=list.size(); i < n; i++)
      list.get(i);code

runs faster than this loop:

速度快於這個循環
    for (Iterator i=list.iterator(); i.hasNext(); )
      i.next();

能夠使用

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, Serializable測試一下。
相關文章
相關標籤/搜索