# ArrayList 无参构造器创建实例 ```java // 源码:无参构造器 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } // add // 1、先确定是否扩容 2、然后再执行赋值 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } // 该方法确定最小容量 minCapacity (1)第一次扩容为10 private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } ensureExplicitCapacity(minCapacity); } // 1、modCount++ 记录集合被修改的次数 // 2、如果 elementData大小不够 就调用grow 扩容 private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } // 底层真正实现扩容的 方法 private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; // 右移一位相当于 除以2 新的为 旧的1.5倍 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); } ```