ArrayList
构造方法
无参构造方法
无参构造方法中将集合长度定义为{}
public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
有参构造
穿入一个Collection集合对象
先将集合转换成Object数组,对数组长度进行判断然后反射看是否为ArrayList集合,是直接赋值,不是则调用本地方法System.arraycopy将元素赋值给初始化数组
public ArrayList(Collection<? extends E> c) { Object[] a = c.toArray(); if ((size = a.length) != 0) { if (c.getClass() == ArrayList.class) { elementData = a; } else { elementData = Arrays.copyOf(a, size, Object[].class); } } else { // replace with empty array. elementData = EMPTY_ELEMENTDATA; } }
没懂方法
public void trimToSize() { modCount++; if (size < elementData.length) { elementData = (size == 0) ? EMPTY_ELEMENTDATA : Arrays.copyOf(elementData, size); } }
ensureCapacity(int minCapacity){}指定元素个数
判断集合数据长度是否为0,不为0则最小期望是0,否则为10
如果输入最小长度大于最小的期望,则调用ensureExplicitCapacity
public void ensureCapacity(int minCapacity) { int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) // any size if not default element table ? 0 // larger than default for default empty table. It's already // supposed to be at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } }
grow(int minCapacity)数组增大
扩大为1.5倍,最大为Integer最大数
复制元素长度,将新长度设为老长度的1.5倍。
如果新元素小于传入数字,则新长度为传入数字,否则为将长度与本类中最大数(本类最大数为Integer最大数-8)比较,如果大于则将Integer最大值赋予该数,然后调用系统复制
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); }
int indexOf(Object o)
判断o是否为null,为null则遍历elementData用==判断,否则遍历用equals判断,没找到返回-1
public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
Object clone(){}
生成新对象,调用系统中拷贝Array.copyof将所有数据拷贝在新的数组中,并初始化modCount,否则报错
public Object clone() { try { ArrayList<?> v = (ArrayList<?>) super.clone(); v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(e); } }
add()
每次add方法将数组需要调用方法判断size+1是否比现在数组长度大,不大则不扩容
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
E remove(int index)方法
remove方法删除元素,但不会懂数组容量
public E remove(int index) { rangeCheck(index); modCount++; E oldValue = elementData(index); int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work return oldValue; }
fastRemove(int index)
直接调用本地方法System.arraycopy复制将数组进行覆盖处理
private void fastRemove(int index) { modCount++; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work }
addAll(Collection<? extends E> c)
将c集合转换成Object类数组然后调用本地方法System.arraycopy()对将得到数组进行复制在原数组上
public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; }
保留集合
public boolean retainAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, true); }
删除集合
public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, false); }
boolean batchRemove(Collection<?> c, boolean complement)
为删除结合和保留集合服务,ture为删除集合,false为保留集合
private boolean batchRemove(Collection<?> c, boolean complement) { //转成数组 final Object[] elementData = this.elementData; int r = 0, w = 0; boolean modified = false; try { //对元数组进行遍历 for (; r < size; r++) //如果complement为true则将c中包含集合元素放入0索引,即将与同于c的元素进行储存,不同的进行删除 //如果为flase则将原数组中不同于c的元素进行储存,相同的进行删除 if (c.contains(elementData[r]) == complement) elementData[w++] = elementData[r]; } finally { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } //将空的删除 if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; }