111 private transient Object[] elementData;
118 private int size;
Constructs an empty list with an initial capacity of ten. 137 138 public ArrayList() { 139 this(10); 140 }
127 public ArrayList(int initialCapacity) { 128 super(); 129 if (initialCapacity < 0) 130 throw new IllegalArgumentException("Illegal Capacity: "+ 131 initialCapacity); 132 this.elementData = new Object[initialCapacity]; 133 }
178 public void ensureCapacity(int minCapacity) { 179 modCount++; 180 int oldCapacity = elementData.length; 181 if (minCapacity > oldCapacity) { 182 Object oldData[] = elementData; 183 int newCapacity = (oldCapacity * 3)/2 + 1; 184 if (newCapacity < minCapacity) 185 newCapacity = minCapacity; 186 // minCapacity is usually close to size, so this is a win: 187 elementData = Arrays.copyOf(elementData, newCapacity); 188 } 189 }
377 public boolean add(E e) { 378 ensureCapacity(size + 1); // Increments modCount!! 379 elementData[size++] = e; 380 return true; 381 }
392 public void add(int index, E element) { 393 rangeCheckForAdd(index); 394 395 ensureCapacity(size+1); // Increments modCount!! 396 System.arraycopy(elementData, index, elementData, index + 1, 397 size - index); 398 elementData[index] = element; 399 size++; 400 }
577 private void rangeCheckForAdd(int index) { 578 if (index > size || index < 0) 579 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 580 }
411 public E remove(int index) { 412 rangeCheck(index); 413 414 modCount++; 415 E oldValue = elementData(index); 416 417 int numMoved = size - index - 1; 418 if (numMoved > 0) 419 System.arraycopy(elementData, index+1, elementData, index, 420 numMoved); 421 elementData[--size] = null; // Let gc do its work 422 423 return oldValue; 424 }
569 private void rangeCheck(int index) { 570 if (index >= size) 571 throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); 572 }
348 public E get(int index) { 349 rangeCheck(index); 350 351 return elementData(index); 352 }
95 private transient Entry<E> header = new Entry<E>(null, null, null); 96 private transient int size = 0;
101 public LinkedList() { 102 header.next = header.previous = header; 103 }
214 public boolean add(E e) { 215 addBefore(e, header); 216 return true; 217 }
794 private Entry<E> addBefore(E e, Entry<E> entry) { 795 Entry<E> newEntry = new Entry<E>(e, entry, entry.previous); 796 newEntry.previous.next = newEntry; 797 newEntry.next.previous = newEntry; 798 size++; 799 modCount++; 800 return newEntry; 801 }
331 public E get(int index) { 332 return entry(index).element; 333 }
380 private Entry<E> entry(int index) { 381 if (index < 0 || index >= size) 382 throw new IndexOutOfBoundsException("Index: "+index+ 383 ", Size: "+size); 384 Entry<E> e = header; 385 if (index < (size >> 1)) { 386 for (int i = 0; i <= index; i++) 387 e = e.next; 388 } else { 389 for (int i = size; i > index; i--) 390 e = e.previous; 391 } 392 return e; 393 }
232 public boolean remove(Object o) { 233 if (o==null) { 234 for (Entry<E> e = header.next; e != header; e = e.next) { 235 if (e.element==null) { 236 remove(e); 237 return true; 238 } 239 } 240 } else { 241 for (Entry<E> e = header.next; e != header; e = e.next) { 242 if (o.equals(e.element)) { 243 remove(e); 244 return true; 245 } 246 } 247 } 248 return false; 249 }
803 private E remove(Entry<E> e) { 804 if (e == header) 805 throw new NoSuchElementException(); 806 807 E result = e.element; 808 e.previous.next = e.next; 809 e.next.previous = e.previous; 810 e.next = e.previous = null; 811 e.element = null; 812 size--; 813 modCount++; 814 return result; 815 }
做者:jiajun 出處: http://www.cnblogs.com/-new/
本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接,不然保留追究法律責任的權利。若是以爲還有幫助的話,能夠點一下右下角的【推薦】,但願可以持續的爲你們帶來好的技術文章!想跟我一塊兒進步麼?那就【關注】我吧。html