package com.song.source.java.util; import com.song.source.java.Collection; import java.io.Serializable; import java.util.AbstractList; import java.util.Iterator; import java.util.RandomAccess; public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable { /** * 默认初始化大小 */ private static final int DEFAULT_CAPACITY = 10; /** * 用于空实例的共享空数组实例。 */ private static final Object[] EMPTY_ELEMENTDATA = {}; /** * 用于默认大小空实例的共享空数组实例。 我们从EMPTY_ELEMENTDATA区分 , 这个知道多少膨胀时添加第一个元素。 */ private static final Object[] DEFAULTPACITY_EMPTY_ELEMENTDATA = {}; /** * ArrayList的元素存储在其中的数组缓冲区。 */ transient Object[] elementData; /** * 初始化集合容器 */ public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity:" + initialCapacity); } } /** * 构造一个初始容量为10的空集合, */ public ArrayList() { this.elementData = DEFAULTPACITY_EMPTY_ELEMENTDATA; } /** * 集合的大小 */ private int size; /** * 返回集合大小 */ public int size() { return size; } /** * 判断集合是不是空 */ public boolean isEmpty() { return size == 0; } public boolean add(E e) { return false; } /** * 获得角标对应容器的值 */ public E get(int index) { // 角标范围检查 rangeCheck(index); return elementData(index); } /** * 角标访问检查 */ private void rangeCheck(int index) { if (index >= size) { throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } } /** * 返回数组对应索引元素 */ private E elementData(int index) { return (E) elementData[index]; } /** * 索引、size打印方法 */ private String outOfBoundsMsg(int index) { return "index :" + index + ", size: " + size; } /** * 查找对应元素,指定位置 */ 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; }
/** * 从该列表中删除指定元素的第一个,如果存在的话。 * 基本原理: * 1.分null和正常数值处理 * 2.遍历集合,判断相等 ,执行删除 * @param o * @return */ public boolean remove(Object o) { if(o == null) { for(int index = 0; index < size; index++) { if(elementData(index) == null) { // 执行删除 fastRemove(index); return true; } } } else { for (int index = 0; index < size; index++) { if(o.equals(elementData(index))) { // 执行删除 fastRemove(index); return true; } } } return false; } /** * 根据给定角标,快速删除集合元素 * 基本原理: * 1。利用数组拷贝的原理。 * 将索引后一位对应的元素,整体移动到 以索引开始的位置上,最后空出来的元素置空,集合长度 -1 * @param index */ 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; }
/** * 清空容器 * 基本思想: * 记录修改次数 * 遍历集合,将每个元素置为空,集合大小置为0 */ public void clear() { modCount++; for(int i = 0; i < size; i++) { elementData[i] = null; } size = 0; }
/** * 集合中加入新元素 * 基本思想: * 1.确保容器可以容纳新元素 * 确保依据是: * 2.确定新元素位置, [size - 1 + 1], 赋值 * 3.size +1 集合总体大小 +1 * @param e * @return */ public boolean add(E e) { ensureCapacityInternal(size + 1); elementData[size++] = e; return true; }
private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } private void ensureExplicitCapacity(int minCapacity) { modCount++; if (minCapacity - elementData.length > 0) { grow(minCapacity); } } private void grow(int minCapacity) { int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } if (newCapacity - MAX_ARRAY_SIZE > 0) { newCapacity = hugeCapacity(minCapacity); } elementData = Arrays.copyOf(elementData, newCapacity); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) { throw new OutOfMemoryError(); } return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; }