JDK源码学习笔记-ArrayList\Vector
1、ArrayList容量变化的规则
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; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity);} }
public void add(int index, E element) {if (index > size || index < 0) throw new IndexOutOfBoundsException("Index: "+index+", Size: "+size);ensureCapacity(size+1); // Increments modCount!![color=red]System.arraycopy(elementData, index, elementData, index + 1, size - index);[/color]elementData[index] = element;size++; }
private void ensureCapacityHelper(int minCapacity) {int oldCapacity = elementData.length;if (minCapacity > oldCapacity) { Object[] oldData = elementData; int newCapacity = (capacityIncrement > 0) ?(oldCapacity + capacityIncrement) : (oldCapacity * 2); if (newCapacity < minCapacity) {newCapacity = minCapacity; } elementData = Arrays.copyOf(elementData, newCapacity);} }
public synchronized boolean retainAll(Collection<?> c) { return super.retainAll(c); }
ListIterator e2 = ((List) o).listIterator();
ArrayList<Integer> temp1 = new ArrayList<Integer>(); Vector<Integer> temp2 = new Vector<Integer>(); Integer e1 = new Integer(1); Integer e2 = new Integer(2); temp1.add(e1); temp1.add(e2); temp2.add(e1); temp2.add(e2); if(temp2.equals(temp1)) { System.out.println("equals"); }else { System.out.println("not equals"); }