LIST 实现
ArrayList是List接口的一个可变长数组实现。实现了所有List接口的操作,并允许存储null值。除了没有进行同步,ArrayList基本等同于Vector。在Vector中几乎对所有的方法都进行了同步,但ArrayList仅对writeObject和 readObject进行了同步,其它比如add(Object)、remove(int)等都没有同步。
1.存储
ArrayList使用一个Object的数组存储元素。
private transient Object elementData[];
ArrayList实现了java.io.Serializable接口,这儿的transient标示这个属性不需要自动序列化。下面会在 writeObject()方法中详细讲解为什么要这样作。
2.add和remove
Java 代码 收藏代码
1. public boolean add(Object o) { 2. ensureCapacity(size + 1); // Increments modCount!! 3. elementData[size++] = o; 4. return true; 5. } public boolean add(Object o) {ensureCapacity(size + 1); // Increments modCount!!elementData[size++] = o;return true;} 1. public Object remove(int index) { 2. RangeCheck(index); 3. modCount++; 4. Object oldValue = elementData[index]; 5. int numMoved = size - index - 1; 6. if (numMoved > 0) 7. System.arraycopy(elementData, index+1, elementData, index, 8. numMoved); 9. elementData[--size] = null; // Let gc do its work 10. return oldValue; 11. } public Object remove(int index) {RangeCheck(index);modCount++;Object oldValue = elementData[index];int numMoved = size - index - 1;if (numMoved > 0)System.arraycopy(elementData, index+1, elementData, index,numMoved);elementData[--size] = null; // Let gc do its workreturn oldValue;} 1. public boolean add(Object o) 2. 3. public void add(int index, Object element) 4. 5. public boolean addAll(Collection c) 6. 7. public boolean addAll(int index, Collection c) public boolean add(Object o)public void add(int index, Object element)public boolean addAll(Collection c)public boolean addAll(int index, Collection c)
1. public void ensureCapacity(int minCapacity) { 2. modCount++; 3. int oldCapacity = elementData.length; 4. if (minCapacity > oldCapacity) { 5. Object oldData[] = elementData; 6. int newCapacity = (oldCapacity * 3)/2 + 1; 7. if (newCapacity < minCapacity) 8. newCapacity = minCapacity; 9. elementData = new Object[newCapacity]; 10. System.arraycopy(oldData, 0, elementData, 0, size); 11. } 12. } public void ensureCapacity(int minCapacity) {modCount++;int oldCapacity = elementData.length;if (minCapacity > oldCapacity) {Object oldData[] = elementData;int newCapacity = (oldCapacity * 3)/2 + 1;if (newCapacity < minCapacity)newCapacity = minCapacity;elementData = new Object[newCapacity];System.arraycopy(oldData, 0, elementData, 0, size);}} 1. public boolean add(Object o) { 2. ensureCapacity(size + 1); // Increments modCount!! 3. elementData[size++] = o; 4. return true; 5. } public boolean add(Object o) {ensureCapacity(size + 1); // Increments modCount!!elementData[size++] = o;return true;}public Iterator iterator() {return new Itr();}public boolean hasNext() {return cursor != size();} 1. public Object next() { 2. try { 3. Object next = get(cursor); 4. checkForComodification(); 5. lastRet = cursor++; 6. return next; 7. } catch(IndexOutOfBoundsException e) { 8. checkForComodification(); 9. throw new NoSuchElementException(); 10. } 11. } public Object next() {try {Object next = get(cursor);checkForComodification();lastRet = cursor++;return next;} catch(IndexOutOfBoundsException e) {checkForComodification();throw new NoSuchElementException();}} 1. final void checkForComodification() { 2. if (modCount != expectedModCount) 3. throw new ConcurrentModificationException(); 4. } final void checkForComodification() {if (modCount != expectedModCount)throw new ConcurrentModificationException();}private static final long serialVersionUID = 8683452581122892189L;private transient Object elementData[];private int size;
1. private synchronized void writeObject(java.io.ObjectOutputStream s) 2. throws java.io.IOException{ 3. // Write out element count, and any hidden stuff 4. s.defaultWriteObject(); 5. // Write out array length 6. s.writeInt(elementData.length); 7. // Write out all elements in the proper order. 8. for (int i=0; i<size; i++) 9. s.writeObject(elementData[i]); 10. } private synchronized void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException{// Write out element count, and any hidden stuffs.defaultWriteObject();// Write out array lengths.writeInt(elementData.length);// Write out all elements in the proper order.for (int i=0; i<size; i++)s.writeObject(elementData[i]);} 1. private synchronized void readObject(java.io.ObjectInputStream s) 2. throws java.io.IOException, ClassNotFoundException { 3. // Read in size, and any hidden stuff 4. s.defaultReadObject(); 5. // Read in array length and allocate array 6. int arrayLength = s.readInt(); 7. elementData = new Object[arrayLength]; 8. // Read in all elements in the proper order. 9. for (int i=0; i<size; i++) 10. elementData[i] = s.readObject(); 11. }