Jdk 接口类RandomAccess了解

1. 接口说明

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)实现使用的标记接口,用于指示它们支持快速(通常是恒定时间)随机访问。该接口的主要目的是允许通用(一般的)算法改变其行为,以便在应用于随机或顺序访问列表时提供良好的性能。

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.

操纵随机访问的列表最佳的算法(如ArrayList)当应用在顺序访问列表会产生二次行为(如LinkedList)。鼓励(支持)通用(一般的)列表算法在应用算法之前检查给定的列表是否是该接口的实例,如果将算法应用于顺序访问列表,则该算法将提供较差的性能,并在必要时改变他们的行为,以保证可以接受的性能。

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,

可以认识到,随机访问和顺序访问之间的区别通常是模糊的。例如,某些 列表实现提供渐进的线性访问时间,如果它们获得极大的访问时间,但实际上是恒定的访问时间。这样的 列表实现通常应该实现此接口。根据经验,如果对于类的典型实例,列表实现应该实现这个接口,

this loop:

这个循环

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

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
测试一下。
posted @ 2018-01-11 16:23  喜欢和习惯  阅读(348)  评论(0编辑  收藏  举报