public E get(int index){ Objects.checkIndex(index, size); return elementData(index); } public E set(int index, E element){ Objects.checkIndex(index, size); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
Vector get,set方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicsynchronized E get(int index){ if (index >= elementCount) thrownew ArrayIndexOutOfBoundsException(index);
return elementData(index); } publicsynchronized E set(int index, E element){ if (index >= elementCount) thrownew ArrayIndexOutOfBoundsException(index);
E oldValue = elementData(index); elementData[index] = element; return oldValue; }
public E get(int index){ checkElementIndex(index); return node(index).item; } public E set(int index, E element){ checkElementIndex(index); Node<E> x = node(index); E oldVal = x.item; x.item = element; return oldVal; } Node<E> node(int index){ // assert isElementIndex(index);
if (index < (size >> 1)) { Node<E> x = first; for (int i = 0; i < index; i++) x = x.next; return x; } else { Node<E> x = last; for (int i = size - 1; i > index; i--) x = x.prev; return x; } }
publicbooleanadd(E e){ modCount++; add(e, elementData, size); returntrue; } privatevoidadd(E e, Object[] elementData, int s){ if (s == elementData.length) elementData = grow(); elementData[s] = e; size = s + 1; } publicvoidadd(int index, E element){ rangeCheckForAdd(index); modCount++; finalint s; Object[] elementData; if ((s = size) == (elementData = this.elementData).length) elementData = grow(); System.arraycopy(elementData, index, elementData, index + 1, s - index); elementData[index] = element; size = s + 1; } public E remove(int index){ Objects.checkIndex(index, size);
modCount++; E oldValue = elementData(index);
int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // clear to let GC do its work
publicsynchronizedbooleanadd(E e){ modCount++; add(e, elementData, elementCount); returntrue; } privatevoidadd(E e, Object[] elementData, int s){ if (s == elementData.length) elementData = grow(); elementData[s] = e; elementCount = s + 1; } publicsynchronized E remove(int index){ modCount++; if (index >= elementCount) thrownew ArrayIndexOutOfBoundsException(index); E oldValue = elementData(index);
int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--elementCount] = null; // Let gc do its work
publicvoidadd(int index, E element){ checkPositionIndex(index);
if (index == size) linkLast(element); else linkBefore(element, node(index)); } public E remove(int index){ checkElementIndex(index); return unlink(node(index)); } voidlinkLast(E e){ final Node<E> l = last; final Node<E> newNode = new Node<>(l, e, null); last = newNode; if (l == null) first = newNode; else l.next = newNode; size++; modCount++; } voidlinkBefore(E e, Node<E> succ){ // assert succ != null; final Node<E> pred = succ.prev; final Node<E> newNode = new Node<>(pred, e, succ); succ.prev = newNode; if (pred == null) first = newNode; else pred.next = newNode; size++; modCount++; } E unlink(Node<E> x){ // assert x != null; final E element = x.item; final Node<E> next = x.next; final Node<E> prev = x.prev;
if (prev == null) { first = next; } else { prev.next = next; x.prev = null; }
if (next == null) { last = prev; } else { next.prev = prev; x.next = null; }
publicstaticvoidmain(String[] args){ List<Integer> list = new ArrayList(); //List<Integer> list = new LinkedList<Integer>(); //Vector<Integer> list=new Vector<>(); for (int i = 0; i < 2000000; i++) { list.add(i); }