ArrayList源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 public class MyArrayList <E > implements Iterable <E > { private static final int DEFAULT_CAPACITY = 10 ; transient Object[] elementData; private static final Object[] EMPTY_ELEMENTDATA = {}; private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; protected transient int modCount = 0 ; private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8 ; private int size; public MyArrayList () { this .elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } public MyArrayList (int initialCapacity) { if (initialCapacity > 0 ) { this .elementData = new Object[initialCapacity]; } else if (initialCapacity == 0 ) { this .elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("容量错误" ); } } public void add (int index, E element) { } public boolean add (E e) { ensureCapacityInternal(size + 1 ); elementData[size++] = e; return true ; } private static int calculateCapacity (Object[] elementData, int minCapacity) { if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) { return Math.max(DEFAULT_CAPACITY, minCapacity); } return minCapacity; } private void ensureCapacityInternal (int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, 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); } 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 ; } public void fastRemove (int index) { modCount++; int numMoved = size - index - 1 ; if (numMoved > 0 ) { System.arraycopy(elementData, index + 1 , elementData, index, numMoved); } elementData[--size] = null ; } private static int hugeCapacity (int minCapaCity) { if (minCapaCity < 0 ) { throw new OutOfMemoryError(); } return minCapaCity > MAX_ARRAY_SIZE ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } @Override public Iterator<E> iterator () { return null ; } private class Itr implements Iterator <E > { int curosr; int lastRet = -1 ; int expectedModCount = modCount; Itr() { } @Override public boolean hasNext () { return curosr != size; } @Override public E next () { checkForComodification(); int i = curosr; if (i >= size) { throw new NoSuchElementException(); } Object[] elementData = MyArrayList.this .elementData; if (i >= elementData.length) { throw new ConcurrentModificationException(); } curosr = i + 1 ; return (E) elementData[lastRet = i]; } @Override public void remove () { if (lastRet < 0 ) { throw new IllegalStateException(); } checkForComodification(); MyArrayList.this .remove(lastRet); curosr = lastRet; lastRet = -1 ; expectedModCount = modCount; } private void checkForComodification () { if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } } } }