- 实现了List接口,顺序的容器;
- 未实现同步,其余与vector类似的设置;
- 允许加入
null元素; ArrayList扩容规则也是扩容1.5倍,先复制原数组,然后放到新的空间。
ArrayList<Object> 实际上是一个语法糖,所以 ArrayList 可以存放所有的对象,因为运行时的类型实际上是 Object 。
底层的实现
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
transient 关键字用于去除自动序列化,因为在传输的过程中只需要传输实际容量的元素,而不用传输整个 capacity 的元素。
扩容实现
删除元素
- 删除指定位置 :
remove(int index) - 删除指定的值的元素:
remove(Object o)
最后为了能让被删除的元素被快速GC,将remove指向的元素手动设置为 null
快速失败
为了在并发过程中,防止有多个线程修改了集合中的元素,当发现这个情况时,直接抛出 ConcurrentModificationException 异常
在每个基本操作过程中都设置一个
modCount 的变量,当执行结束之后的 expectCount 不一致时,很容易发现出现了同步的异常。
评论(0)
暂无评论