资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java中RandomAccess接口源码分析

java 中RandomAccess接口源码分析

创新互联公司服务项目包括江北网站建设、江北网站制作、江北网页制作以及江北网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,江北网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到江北省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

RandomAccess是一个接口,位于java.util包中。

这个接口的作用注释写的很清楚了:

/**
 * 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实现所使用的标记接口,用来表明实现了这些接口的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)的最佳算法在应用于顺序存取列表时,有可能产生二次项行为。 * 泛型算法列表鼓励在将某个算法应用于顺序存取列表可能导致差的性能之前,先检查给定的列表是否是这个接口的一个实例, * 并在需要时去改变这些算法的行为以保证性能。 *

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: * 随机访问和顺序存取之间的界限通常是模糊的。例如,一些List实现在变得很大时会导致渐进的非线性访问时间,但实际上是常量访问时间。 * 这样的List实现通常都应该实现该接口。 * 一般来说,某个List实现如果(对某些典型的类的实例来说)满足下面的条件,就应该实现这个接口:循环 *

 *   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();
 * 
* *

This interface is a member of the * * Java Collections Framework. * 这个接口是Java集合框架的一员。 * @since 1.4 */ public interface RandomAccess { }

 RandomAccess是一个空接口,而空接口的作用一般是起到一个标识的作用。

通俗点讲,就是判断一个list是否实现了RandomAcess接口,如果实现了,采用下面所示的简单的for循环进行访问速度比较快:

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

如果未实现RandomAcess接口,则采用下面的iterator循环访问速度比较快。

for (Iterator i=list.iterator(); i.hasNext(); )
   i.next();

判断使用instanceof,即

 if (list instanceof RandomAccess) 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


网站题目:java中RandomAccess接口源码分析
网站路径:http://cdkjz.cn/article/gcpigi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220