好吧,我们知道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 behaviour to provide good performance when applied to either random or sequential access lists.
因此,对我来说,ArrayList实现RandomAccess接口是完全合理的,因为内部元素存储在一个可以随机访问的数组中.但是,如果您将看到ArrayDeque的内部实现,它也将元素存储在一个数组中,但它没有实现RandomAccess,它是否有意或者它是由一些我不知道的明确原因完成的?
解决方法:
List实现使用RandomAccess来指示它们支持快速随机访问.
ArrayDeque不是List,也没有任何随机访问方法(没有基于索引的add,get,remove或set方法),所以将它作为RandomAccess是没有意义的.
底层结构可能是随机访问,但该类不允许随机访问.
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!