ArrayList

ArrayList本质上是用数组存储数据。

 1: public class ArrayList<E> extends AbstractList<E> implements List<E>,RandomAccess, Cloneable, java.io.Serializable {
 2: private static final long serialVersionUID = 8683452581122892189L;
 3: // 初始化时默认的容器大小
 4: private static final int DEFAULT_CAPACITY = 10;
 5: /**
 6:  * Shared empty array instance used for empty instances.
 7:  */
 8: private static final Object[] EMPTY_ELEMENTDATA = {};
 9:  
 10: //当类被串行化的时候,transient型变量的值不包括在串行化的表示中
 11: transient Object[] elementData; // non-private to simplify nested 
 12:  
 13: //实际元素个数
 14: private int size;

 

构造函数:

 1: public ArrayList() {
 2: super();//用了父亲类,这个父亲类方法体是空的
 3: this.elementData = EMPTY_ELEMENTDATA;
 4: }
 5:  
 6: //如果知道数组的长度,此时最好使用new ArrayList(数组实际长度)):
 7: // 构造函数,可指定容量
 8: public ArrayList(int initialCapacity) {
 9: super();
 10: if (initialCapacity < 0)
 11: throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
 12: this.elementData = new Object[initialCapacity];
 13: }
 14:  
 15: public ArrayList(Collection<? extends E> c) {
 16: elementData = c.toArray();
 17: size = elementData.length;
 18: // c.toArray might (incorrectly) not return Object[] (see 6260652)
 19: if (elementData.getClass() != Object[].class)
 20: elementData = Arrays.copyOf(elementData, size, Object[].class);
 21: }

 

部分方法的实现:

 1: //添加元素
 2: public boolean add(E e) {
 3: ensureCapacityInternal(size + 1); // Increments modCount!!
 4: elementData[size++] = e;
 5: return true;
 6: }
 7: private void ensureCapacityInternal(int minCapacity) {
 8: if (elementData == EMPTY_ELEMENTDATA) {
 9: minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);//取默认容量与当前容量的最大值
 10: }
 11: ensureExplicitCapacity(minCapacity);
 12: }
 13: private void ensureExplicitCapacity(int minCapacity) {
 14: modCount++;
 15: //The number of times this list has been structurally modified
 16: //表示list被修改的次数
 17: //AbstractList类的成员变量 
 18: if (minCapacity - elementData.length > 0)
 19: grow(minCapacity);
 20: }
 21: private void grow(int minCapacity) {
 22: int oldCapacity = elementData.length;
 23: int newCapacity = oldCapacity + (oldCapacity >> 1); //newCapacity为elementData.length的1.5倍
 24: if (newCapacity - minCapacity < 0)//当前元素个数的1.5倍大于当前容量,按当前容量copy
 25: newCapacity = minCapacity;
 26: if (newCapacity - MAX_ARRAY_SIZE > 0)//当前元素个数的1.5倍大于最大容量,hugeCapacity
 27: newCapacity = hugeCapacity(minCapacity);
 28: elementData = Arrays.copyOf(elementData, newCapacity);
 29: }
 30: private static int hugeCapacity(int minCapacity) {
 31: if (minCapacity < 0) 
 32: throw new OutOfMemoryError();
 33: return (minCapacity > MAX_ARRAY_SIZE) ?
 34: Integer.MAX_VALUE :
 35: MAX_ARRAY_SIZE;
 36: }
 37:  
 38: private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
 39:  
 40: public void add(int index, E element) {
 41: rangeCheckForAdd(index);//检测index
 42: ensureCapacityInternal(size + 1); 
 43: System.arraycopy(elementData, index, elementData, index + 1, size - index);
 44: elementData[index] = element;
 45: size++;
 46: }
 47:  
 48: private void rangeCheckForAdd(int index) {
 49: if (index > size || index < 0)
 50: throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 51: }
 52:  
 53: public E get(int index) {
 54: rangeCheck(index);
 55: return elementData(index);
 56: }
 57: private void rangeCheck(int index) {
 58: if (index >= size)
 59: throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
 60: }
 61:  
 62:  
 63: public boolean contains(Object o) {
 64: return indexOf(o) >= 0;
 65: }
 66:  
 67: public int indexOf(Object o) {
 68: if (o == null) {
 69: for (int i = 0; i < size; i++)
 70: if (elementData[i]==null)
 71: return i;
 72: } else {
 73: for (int i = 0; i < size; i++)
 74: if (o.equals(elementData[i]))
 75: return i;
 76: }
 77: return -1;
 78: }
 79:  
 80: public E remove(int index) {
 81: rangeCheck(index);
 82: modCount++;
 83: E oldValue = elementData(index);
 84:  
 85: int numMoved = size - index - 1;
 86: if (numMoved > 0)
 87: System.arraycopy(elementData, index+1, elementData, index, numMoved);
 88: elementData[--size] = null; // clear to let GC do its work
 89: return oldValue;
 90: }
 91:  
 92: public boolean remove(Object o) {
 93: if (o == null) {
 94: for (int index = 0; index < size; index++)
 95: if (elementData[index] == null) {
 96: fastRemove(index);
 97: return true;
 98: }
 99: } else {
 100: for (int index = 0; index < size; index++)
 101: if (o.equals(elementData[index])) {
 102: fastRemove(index);
 103: return true;
 104: }
 105: }
 106: return false;
 107: }
 108:  
 109: private void fastRemove(int index) {
 110: modCount++;
 111: int numMoved = size - index - 1;
 112: if (numMoved > 0)
 113: System.arraycopy(elementData, index+1, elementData, index, numMoved);
 114: elementData[--size] = null; // clear to let GC do its work
 115: }
 116:  
 117: public void clear() {
 118: modCount++;
 119: // clear to let GC do its work
 120: for (int i = 0; i < size; i++)
 121: elementData[i] = null;
 122: size = 0;
 123: }
 124:  
 125:  
 126: //System.arraycopy是浅拷贝
 127: public Object clone() {
 128: try {
 129: ArrayList<?> v = (ArrayList<?>) super.clone();
 130: v.elementData = Arrays.copyOf(elementData, size);
 131: v.modCount = 0;
 132: return v;
 133: } catch (CloneNotSupportedException e) {
 134: // this shouldn't happen, since we are Cloneable
 135: throw new InternalError(e);
 136: }
 137: }

 

你可能感兴趣的:(ArrayList)