* 这个接口的主要目的是允许一般的算法更改它们的行为,以便在随机或者顺序存取列表时能提供更好的性能。
<span style="color: #008000;"> * <p>The best algorithms for manipulating random access lists (such as * <tt>ArrayList</tt>) can produce quadratic behavior when applied to * sequential access lists (such as <tt>LinkedList</tt>). Generic list * algorithms are encouraged to check whether the given list is an * <tt>instanceof</tt> 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. * </span>操作随机访问列表(如ArrayList)的最佳算法在应用于顺序存取列表时,有可能产生二次项行为。<br /> * 泛型算法列表鼓励在将某个算法应用于顺序存取列表可能导致差的性能之前,先检查给定的列表是否是这个接口的一个实例,<br /><span style="color: #000000;"> * 并在需要时去改变这些算法的行为以保证性能。</span>
<span style="color: #008000;"> * <p>It is recognized that the distinction between random and sequential * access is often fuzzy. For example, some <tt>List</tt> implementations * provide asymptotically linear access times if they get huge, but constant * access times in practice. Such a <tt>List</tt> implementation * should generally implement this interface. As a rule of thumb, a * <tt>List</tt> implementation should implement this interface if, * for typical instances of the class, this loop:<br /><br /></span> * 随机访问和顺序存取之间的界限通常是模糊的。例如,一些List实现在变得很大时会导致渐进的非线性访问时间,但实际上是常量访问时间。<br /> * 这样的List实现通常都应该实现该接口。
* 一般来说,某个List实现如果(对某些典型的类的实例来说)<span style="color: #ff6600;">满足下面的条件,就应该实现这个接口</span>:循环
<span style="color: #008000;"> * <pre>
* for (int i=0, n=list.size(); i < n; i++)
* list.get(i);
* </pre>
* runs faster than this loop:<br /> <span style="color: #000000;">* 比下面的循环运行速度快。</span>
* <pre>
* for (Iterator i=list.iterator(); i.hasNext(); )
* i.next();
* </pre>
*
* <p>This interface is a member of the
* <a href="{@</span><span style="color: #808080;">docRoot</span><span style="color: #008000;">}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
* <span style="color: #000000;">这个接口是Java集合框架的一员。</span>
* </span><span style="color: #808080;">@since</span><span style="color: #008000;"> 1.4
</span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> RandomAccess {
}</span>
RandomAccess是一个空接口,而空接口的作用一般是起到一个标识的作用。
通俗点讲,就是判断一个list是否实现了RandomAcess接口,如果实现了,采用下面所示的简单的for循环进行访问速度比较快:
<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i=0, n=list.size(); i < n; i++<span style="color: #000000;">)
list.get(i);</span>
如果未实现RandomAcess接口,则采用下面的iterator循环访问速度比较快。
<span style="color: #0000ff;">for</span> (Iterator i=<span style="color: #000000;">list.iterator(); i.hasNext(); )
i.next();</span>
判断使用instanceof,即
<span style="color: #0000ff;">if</span> (list <span style="color: #0000ff;">instanceof</span> RandomAccess)
RandomAccess接口
标签:port ceo rod ide style 性能 size guide tween
本文系统来源:http://www.cnblogs.com/songwenlong/p/6901669.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!