存储结构
- transient Object[] elementdata
- DEFAULT_CAPACITY =10
初始化
-
默认构造器初始化
-
带参构造器
-
int initcapacity
-
ArrayList(Collection<? extends E> c)
-
主要操作
-
添加数据
- add(E e)
- add(int index, E e)
- addAll(Collection<? extends E> c)
- addAll(int index, Collection<? extends E> c)
- index 不能大于 size
-
删除数据
- remove(int index)
- remove(Obejct obj)
- removeAll(Collection<? extends E> c), 删除list中与集合C 中相同的元素
- removeIf(Predicate<? super E> filter) ,通过filter 删掉满足条件的元素
- clear()
-
查询数据
- get(int index) ,根据索引进行查询
- contains(Object obj) ,查询是否存在相关对象
- indexOf(Objext o)
-
修改数据
- set(int index, Object obj),
- replaceAll(UnaryOperator
operator)
-
排序
- sort(Comparator<? extends super E> c)
-
扩容
private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }