为什么 ArrayList RandomAccess 而 ArrayDeque 不是?
Why is ArrayList RandomAccess but ArrayDeque not?
嗯,我们知道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
,那么它是有意为之还是出于某种明确的原因 which我不知道?
RandomAccess
被 List
实现用来表示它们支持快速随机访问。
ArrayDeque
不是 List
并且没有任何随机访问方法(没有基于索引的 add
、get
、remove
或 set
方法),因此将其设置为 RandomAccess
是没有意义的。
底层结构可能是随机访问,但class不允许随机访问。
嗯,我们知道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
,那么它是有意为之还是出于某种明确的原因 which我不知道?
RandomAccess
被 List
实现用来表示它们支持快速随机访问。
ArrayDeque
不是 List
并且没有任何随机访问方法(没有基于索引的 add
、get
、remove
或 set
方法),因此将其设置为 RandomAccess
是没有意义的。
底层结构可能是随机访问,但class不允许随机访问。