RandomAccess接口

该接口是一个标记接口,本身没有任何方法。

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.

  • 是一个标记接口,实现该接口的列表说明其支持随机访问。(通常是常数时间的)
    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.
  • 一般的列表访问算法在访问列表元素之前,都被建议先使用instanceof关键字检查一下列表是否是一个RandomAccess子类,然后再决定采用随机还是顺序方式访问列表中的元素,这样可以保证访问算法拥有最佳的性能。
    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子类应该实现RandomAccess接口。
    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();

若是集合的for循环访问数据比使用iterator访问来的高效快速,那么最好去实现RandomAccess这个标记接口,这样在一些框架代码中,就可以根据是否实现了RandomAccess接口做出更好的决策方式。

举例子:
不是说给你个2米的标记,你身高就真的能两米了。是你2米了,我就给你个两米的标记。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值