private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
private static final int DEFAULT_CAPACITY = 10; //默认容量 10
构造方法:
1 public ArrayList() { //无参构造,默认容量_空的_elementData 2 this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; 3 } 4 5 public ArrayList(int initialCapacity) { //给一个初始的容量 6 if (initialCapacity > 0) { 7 this.elementData = new Object[initialCapacity]; 8 } else if (initialCapacity == 0) { 9 this.elementData = EMPTY_ELEMENTDATA; 10 } else { 11 throw new IllegalArgumentException("Illegal Capacity: "+ 12 initialCapacity); 13 } 14 } 15 16 public ArrayList(Collection<? extends E> c) { //给一个Collection 17 elementData = c.toArray(); //转成数组 交给elementData 18 if ((size = elementData.length) != 0) { //如果长度不为0 19 // c.toArray might (incorrectly) not return Object[] (see 6260652) 20 //c.toArray()可能不会返回一个Object数组,再次检查,如果不是,就会copy一份 至 Object数组 并交给elementData数组 21 if (elementData.getClass() != Object[].class) 22 elementData = Arrays.copyOf(elementData, size, Object[].class); 23 } else { 24 // replace with empty array. 25 this.elementData = EMPTY_ELEMENTDATA; //如果长度为0 ,则以EMPTY_ELEMENTDATA代替空的数组 26 } 27 }
add();
1 public boolean add(E e) { 2 ensureCapacityInternal(size + 1); // 确保还有容量添加元素 3 elementData[size++] = e;//添加元素 4 return true; 5 } 6 7 private void ensureCapacityInternal(int minCapacity) { 8 //判断 elementData 是不是通过默认构造来的, 9 //是的话就 从 (10,size+1)中取较大的作为初始容量 10 if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { 11 minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); 12 } 13 14 ensureExplicitCapacity(minCapacity); 15 } 16 //Explicit:明确的、清楚的 17 private void ensureExplicitCapacity(int minCapacity) { 18 modCount++; 19 21 //如果是默认构造,minCapacity = 10,首次必定会初始化elementData数组 22 //判断是否需要扩容, 如果需要的容量(size+1) >当前数组的长度,就是要扩容 23 if (minCapacity - elementData.length > 0) 24 grow(minCapacity); 25 } 26 27 private void grow(int minCapacity) { 28 //得到当前数组的长度 29 int oldCapacity = elementData.length; 30 //如果是默认构造, 0 31 //如果是自定义,则为 1.5倍 32 int newCapacity = oldCapacity + (oldCapacity >> 1); 33 if (newCapacity - minCapacity < 0) //如果新容量小于minCapacity(size +1) or 如果是默认构造第一次就是10 34 newCapacity = minCapacity; //则新容量的大小为 minCapacity 35 if (newCapacity - MAX_ARRAY_SIZE > 0) //如果新容量 大于Integer.MAX_VALUE - 8; 36 newCapacity = hugeCapacity(minCapacity); //规定它的最大值 37 //扩容 38 elementData = Arrays.copyOf(elementData, newCapacity); 39 } 40 41 private static int hugeCapacity(int minCapacity) { 42 if (minCapacity < 0) // overflow 43 throw new OutOfMemoryError(); 44 return (minCapacity > MAX_ARRAY_SIZE) ? 45 Integer.MAX_VALUE : 46 MAX_ARRAY_SIZE; 47 }
remove();
1 public E remove(int index) { 2 rangeCheck(index); //检查参数是否正确 3 4 modCount++; 5 E oldValue = elementData(index); // 拿到对应下标的元素 6 7 // E elementData(int index) { 8 // return (E) elementData[index]; 9 //} 10 11 int numMoved = size - index - 1; 12 if (numMoved > 0) 13 System.arraycopy(elementData, index+1, elementData, index, 14 numMoved); 15 //把elementData数组中索引从index+1 开始 复制numMoved个 到 16 //elementData数组中 索引从index开始 17 18 elementData[--size] = null; // clear to let GC do its work 19 20 return oldValue; //返回删除的值 21 } 22 23 public boolean remove(Object o) { 24 if (o == null) { //如果要删除的对象是null,则循环找到一个null删除返回true 25 for (int index = 0; index < size; index++) 26 if (elementData[index] == null) { 27 fastRemove(index); 28 return true; 29 } 30 } else { 31 for (int index = 0; index < size; index++) 32 if (o.equals(elementData[index])) { 33 fastRemove(index); 34 return true; 35 } 36 } 37 return false; 38 } 39 40 private void fastRemove(int index) { //同上 41 modCount++; 42 int numMoved = size - index - 1; 43 if (numMoved > 0) 44 System.arraycopy(elementData, index+1, elementData, index, 45 numMoved); 46 elementData[--size] = null; // clear to let GC do its work 47 }
IndexOf()
1 public int indexOf(Object o) { //循环找 2 if (o == null) { 3 for (int i = 0; i < size; i++) 4 if (elementData[i]==null) 5 return i; 6 } else { 7 for (int i = 0; i < size; i++) 8 if (o.equals(elementData[i])) 9 return i; 10 } 11 return -1; 12 } 13 14 //contains实际就是调用了indexOf() 15 public boolean contains(Object o) { 16 return indexOf(o) >= 0; 17 }