ArrayList是按照线性表结构实现的 ArrayList的主要继承结构 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable public interface List<E> extends Collection<E> public interface Collection<E> extends Iterable<E> public interface Iterable<T>
1.属性
private static final long serialVersionUID = 8683452581122892189L;//序列化ID private static final int DEFAULT_CAPACITY = 10;//默认初始化数组的长度是10 private static final Object[] EMPTY_ELEMENTDATA = {};//空数组,在构造时,如果参数是0,就用它 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};//空数组,在构造时,如果没有参数,就用它 transient Object[] elementData; //arrayList存放数据的数组 //将不需要序列化的属性前添加关键字transient,序列化对象的时候,这个属性就不会序列化到指定的目的地中。 private int size; //arrayList的长度 默认是0
2.构造器
提供了3个构造器
//指定数组默认初始化的长度//如果大于0,就创建一个指定长度的数组//如果等于0,就默认是一个空数组 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); } } //如果不指定任何参数,就默认是一个空数组 public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } //把其他的Collection对象放到它这里,如果长度是0,就指定一个空数组;否则把传入对象的所有数据一一拷贝到新数组里 public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); if ((size = elementData.length) != 0) { // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); } else { // replace with empty array. this.elementData = EMPTY_ELEMENTDATA; } }
3.添加数据
动态扩容策略
public boolean add(E e) { ensureCapacityInternal(size + 1); //先给数组扩容,如果空间够用,就不用扩;否则按照一定的策略进行扩容 elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { //如果当前的数组是DEFAULTCAPACITY_EMPTY_ELEMENTDATA,就初始化数组的长度 = MAX(10 , size+1)
//使用数组DEFAULTCAPACITY_EMPTY_ELEMENTDATA时,说明使用的是没有参数的构造器
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity); } //最后给数组扩容 ensureExplicitCapacity(minCapacity); } 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; int newCapacity = oldCapacity + (oldCapacity >> 1);//数组扩容策略:增加原数组长度左移位(oldCapacity >> 1) if (newCapacity - minCapacity < 0)//如果新增的数组长度还是小于数据的个数,就把新数组的长度(newCapacity)等于数据的个数(minCapacity) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0)//如果新数组的长度大于最大数组长度说明数组的长度也忒大了,就启动超级扩容策略hugeCapacity() newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity);//最后按照newCapacity的长度,把原先的数据拷贝到新数组 } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : //2147483647 MAX_ARRAY_SIZE; //2147483639 //数组的最大长度就是Integer.MAX_VALUE(2147483647) } private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; /****************************************************************/ Arrays.class:
public static int[] copyOf(int[] original, int newLength) {
int[] copy = new int[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
4.整理数组
//如果当前数组的长度大于数据存放的长度,那么就把当前数组换成新数组( 按照数据存放的长度重新创建的数组,然后把数据放到新数组里 )//这样能有效节省数组空间 public void trimToSize() { modCount++; if (size < elementData.length) { elementData = (size == 0) ? EMPTY_ELEMENTDATA : Arrays.copyOf(elementData, size); } }
时间: 2024-10-06 19:44:19