使用 Array (数组) 作为内部存储:
RandomAccess:
RandomAccess
接口标记接口
,空接口,用于告诉其他类,本类支持快速随机访问
Collection.binarySearch()
二分查找JDK 中 RandomAccess
接口的部分 JavaDoc:
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.
......
As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
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();
类似的标记接口
有:Cloneable
、Serializable
ArrayList
没有大小限制,默认和传入的容量值是指初始值// 默认初始容量为10
private static final int DEFAULT_CAPACITY = 10;
// 用于当外部指定容量为0时,或者size为0的时候进行trimToSize(),elementData的值
private static final Object[] EMPTY_ELEMENTDATA = {};
// 如果没有指定初始容量时,elementData的值
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
// 存储元素的 Object[] 数组
transient Object[] elementData; // non-private to simplify nested class access
// 当前存在元素的个数
private int size;
transient
:Java 关键字,瞬变的。标识此字段不会参与序列化和反序列化。
Q:但是我们调用 ObjectOutputStream.writeObject()
的时候,发现 ArrayList
中的元素可以被序列化,这是为什么?
A:这是由于 ArrayList
内部实现了 writeObject()
和 readObject()
方法,从而实现自定义序列化和反序列化。
详情请看 Serializable
接口的 JavaDoc。
add(E e):
对于任何进制 n,左移一位等于:乘以 n;右移一位等于:除以 n。
比如:
- 十进制:1 << 1 = 10
- 二进制:01 << 1 = 10(二进制) = 2(十进制)
remove(int index):
关于ConcurrentModificationException
:
底层实现:数组
时间复杂度:
原文:https://www.cnblogs.com/demojie/p/12596154.html