189 |
|
* @param minCapacity the desired minimum capacity |
190 |
|
*/ |
191 |
|
private void growArray(int minCapacity) { |
192 |
< |
if (minCapacity < 0) throw new OutOfMemoryError(); // int overflow |
192 |
> |
if (minCapacity < 0) |
193 |
> |
throw new OutOfMemoryError(); // int overflow |
194 |
|
int oldCapacity = elementData.length; |
195 |
|
// Double size if small; else grow by 50% |
196 |
|
int newCapacity = ((oldCapacity < 64)? |
197 |
< |
(oldCapacity * 2): |
198 |
< |
((oldCapacity * 3)/2)); |
197 |
> |
((oldCapacity + 1) * 2): |
198 |
> |
((oldCapacity * 3) / 2)); |
199 |
|
if (newCapacity < minCapacity) |
200 |
|
newCapacity = minCapacity; |
201 |
|
elementData = Arrays.copyOf(elementData, newCapacity); |
345 |
|
// Positional Access Operations |
346 |
|
|
347 |
|
/** |
348 |
< |
* Throws an appropriate exception for indexing errors. |
348 |
> |
* Returns error message string for IndexOutOfBoundsExceptions |
349 |
|
*/ |
350 |
< |
private static void rangeException(int i, int s) { |
351 |
< |
throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
350 |
> |
private String ioobe(int index) { |
351 |
> |
return "Index: " + index + ", Size: " + size; |
352 |
|
} |
353 |
|
|
354 |
|
/** |
360 |
|
*/ |
361 |
|
public E get(int index) { |
362 |
|
if (index >= size) |
363 |
< |
rangeException(index, size); |
363 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
364 |
|
return (E)elementData[index]; |
365 |
|
} |
366 |
|
|
375 |
|
*/ |
376 |
|
public E set(int index, E element) { |
377 |
|
if (index >= size) |
378 |
< |
rangeException(index, size); |
378 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
379 |
|
|
380 |
|
E oldValue = (E) elementData[index]; |
381 |
|
elementData[index] = element; |
410 |
|
public void add(int index, E element) { |
411 |
|
int s = size; |
412 |
|
if (index > s || index < 0) |
413 |
< |
rangeException(index, s); |
413 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
414 |
|
modCount++; |
415 |
|
if (s >= elementData.length) |
416 |
|
growArray(s + 1); |
432 |
|
public E remove(int index) { |
433 |
|
int s = size - 1; |
434 |
|
if (index > s) |
435 |
< |
rangeException(index, size); |
435 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
436 |
|
modCount++; |
437 |
|
E oldValue = (E)elementData[index]; |
438 |
|
int numMoved = s - index; |
540 |
|
*/ |
541 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
542 |
|
if (index > size || index < 0) |
543 |
< |
throw new IndexOutOfBoundsException( |
543 |
< |
"Index: " + index + ", Size: " + size); |
543 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
544 |
|
|
545 |
|
Object[] a = c.toArray(); |
546 |
|
int numNew = a.length; |
650 |
|
*/ |
651 |
|
public ListIterator<E> listIterator(int index) { |
652 |
|
if (index < 0 || index > size) |
653 |
< |
throw new IndexOutOfBoundsException("Index: "+index); |
653 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
654 |
|
return new ArrayListIterator(index); |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
+ |
* {@inheritDoc} |
659 |
+ |
*/ |
660 |
+ |
public ListIterator<E> listIterator() { |
661 |
+ |
return new ArrayListIterator(0); |
662 |
+ |
} |
663 |
+ |
|
664 |
+ |
/** |
665 |
|
* Returns an iterator over the elements in this list in proper sequence. |
666 |
|
* |
667 |
|
* @return an iterator over the elements in this list in proper sequence |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
< |
* A streamlined version of AbstractList.Itr |
674 |
> |
* A streamlined version of AbstractList.ListItr |
675 |
|
*/ |
676 |
|
final class ArrayListIterator implements ListIterator<E> { |
677 |
|
int cursor; // index of next element to return; |
701 |
|
} |
702 |
|
|
703 |
|
public E next() { |
704 |
< |
if (expectedModCount == modCount) { |
704 |
> |
try { |
705 |
|
int i = cursor; |
706 |
< |
if (i < size) { |
707 |
< |
try { |
708 |
< |
E e = (E)elementData[i]; |
709 |
< |
lastRet = i; |
710 |
< |
cursor = i + 1; |
704 |
< |
return e; |
705 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
706 |
< |
} |
707 |
< |
} |
708 |
< |
} |
709 |
< |
// Prefer reporting CME if applicable on failures |
710 |
< |
if (expectedModCount == modCount) |
706 |
> |
E next = get(i); |
707 |
> |
lastRet = i; |
708 |
> |
cursor = i + 1; |
709 |
> |
return next; |
710 |
> |
} catch (IndexOutOfBoundsException ex) { |
711 |
|
throw new NoSuchElementException(); |
712 |
< |
throw new ConcurrentModificationException(); |
712 |
> |
} finally { |
713 |
> |
if (expectedModCount != modCount) |
714 |
> |
throw new ConcurrentModificationException(); |
715 |
> |
} |
716 |
|
} |
714 |
– |
|
717 |
|
public E previous() { |
718 |
< |
if (expectedModCount == modCount) { |
718 |
> |
try { |
719 |
|
int i = cursor - 1; |
720 |
< |
if (i < size) { |
721 |
< |
try { |
722 |
< |
E e = (E)elementData[i]; |
723 |
< |
lastRet = i; |
724 |
< |
cursor = i; |
723 |
< |
return e; |
724 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
725 |
< |
} |
726 |
< |
} |
727 |
< |
} |
728 |
< |
if (expectedModCount == modCount) |
720 |
> |
E next = get(i); |
721 |
> |
lastRet = i; |
722 |
> |
cursor = i; |
723 |
> |
return next; |
724 |
> |
} catch (IndexOutOfBoundsException ex) { |
725 |
|
throw new NoSuchElementException(); |
726 |
< |
throw new ConcurrentModificationException(); |
726 |
> |
} finally { |
727 |
> |
if (modCount != expectedModCount) |
728 |
> |
throw new ConcurrentModificationException(); |
729 |
> |
} |
730 |
|
} |
731 |
|
|
732 |
|
public void remove() { |