101 |
|
/** |
102 |
|
* Constructs an empty list with the specified initial capacity. |
103 |
|
* |
104 |
< |
* @param initialCapacity the initial capacity of the list |
105 |
< |
* @exception IllegalArgumentException if the specified initial capacity |
106 |
< |
* is negative |
104 |
> |
* @param initialCapacity the initial capacity of the list |
105 |
> |
* @throws IllegalArgumentException if the specified initial capacity |
106 |
> |
* is negative |
107 |
|
*/ |
108 |
|
public ArrayList(int initialCapacity) { |
109 |
|
super(); |
123 |
|
/** |
124 |
|
* Constructs a list containing the elements of the specified |
125 |
|
* collection, in the order they are returned by the collection's |
126 |
< |
* iterator. |
126 |
> |
* iterator. The <tt>ArrayList</tt> instance has an initial capacity of |
127 |
> |
* 110% the size of the specified collection. |
128 |
|
* |
129 |
|
* @param c the collection whose elements are to be placed into this list |
130 |
|
* @throws NullPointerException if the specified collection is null |
131 |
|
*/ |
132 |
|
public ArrayList(Collection<? extends E> c) { |
133 |
< |
Object[] a = c.toArray(); |
134 |
< |
// If c.toArray incorrectly doesn't return Object[], copy it. |
135 |
< |
if (a.getClass() != Object[].class) |
136 |
< |
a = Arrays.copyOf(a, a.length, Object[].class); |
137 |
< |
elementData = a; |
138 |
< |
size = a.length; |
133 |
> |
int size = c.size(); |
134 |
> |
// 10% for growth |
135 |
> |
int cap = ((size/10)+1)*11; |
136 |
> |
if (cap > 0) { |
137 |
> |
Object[] a = new Object[cap]; |
138 |
> |
a[size] = a[size+1] = UNALLOCATED; |
139 |
> |
Object[] b = c.toArray(a); |
140 |
> |
if (b[size] == null && b[size+1] == UNALLOCATED) { |
141 |
> |
b[size+1] = null; |
142 |
> |
elementData = b; |
143 |
> |
this.size = size; |
144 |
> |
return; |
145 |
> |
} |
146 |
> |
} |
147 |
> |
initFromConcurrentlyMutating(c); |
148 |
> |
} |
149 |
> |
|
150 |
> |
private void initFromConcurrentlyMutating(Collection<? extends E> c) { |
151 |
> |
elementData = c.toArray(); |
152 |
> |
size = elementData.length; |
153 |
> |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
154 |
> |
if (elementData.getClass() != Object[].class) |
155 |
> |
elementData = Arrays.copyOf(elementData, size, Object[].class); |
156 |
|
} |
157 |
|
|
158 |
+ |
private final static Object UNALLOCATED = new Object(); |
159 |
+ |
|
160 |
|
/** |
161 |
|
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
162 |
|
* list's current size. An application can use this operation to minimize |
175 |
|
* necessary, to ensure that it can hold at least the number of elements |
176 |
|
* specified by the minimum capacity argument. |
177 |
|
* |
178 |
< |
* @param minCapacity the desired minimum capacity |
159 |
< |
*/ |
160 |
< |
/** |
161 |
< |
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
162 |
< |
* necessary, to ensure that it can hold at least the number of elements |
163 |
< |
* specified by the minimum capacity argument. |
164 |
< |
* |
165 |
< |
* @param minCapacity the desired minimum capacity |
178 |
> |
* @param minCapacity the desired minimum capacity |
179 |
|
*/ |
180 |
|
public void ensureCapacity(int minCapacity) { |
181 |
|
modCount++; |
184 |
|
} |
185 |
|
|
186 |
|
/** |
187 |
< |
* Increase the capacity of the array. |
188 |
< |
* @param minCapacity the desired minimum capacity |
187 |
> |
* Increases the capacity of the array. |
188 |
> |
* |
189 |
> |
* @param minCapacity the desired minimum capacity |
190 |
|
*/ |
191 |
|
private void growArray(int minCapacity) { |
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 + 1)); |
196 |
> |
int newCapacity = ((oldCapacity < 64)? |
197 |
> |
((oldCapacity + 1) * 2): |
198 |
> |
((oldCapacity * 3) / 2)); |
199 |
|
if (newCapacity < minCapacity) |
200 |
|
newCapacity = minCapacity; |
201 |
|
elementData = Arrays.copyOf(elementData, newCapacity); |
344 |
|
|
345 |
|
// Positional Access Operations |
346 |
|
|
347 |
< |
/** |
348 |
< |
* Create and return an appropriate exception for indexing errors |
347 |
> |
/** |
348 |
> |
* Returns error message string for IndexOutOfBoundsExceptions |
349 |
|
*/ |
350 |
< |
private static IndexOutOfBoundsException rangeException(int i, int s) { |
351 |
< |
return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
350 |
> |
private String ioobe(int index) { |
351 |
> |
return "Index: " + index + ", Size: " + size; |
352 |
|
} |
353 |
|
|
338 |
– |
// Positional Access Operations |
339 |
– |
|
354 |
|
/** |
355 |
|
* Returns the element at the specified position in this list. |
356 |
|
* |
360 |
|
*/ |
361 |
|
public E get(int index) { |
362 |
|
if (index >= size) |
363 |
< |
throw 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 |
< |
throw rangeException(index, size); |
378 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
379 |
|
|
380 |
|
E oldValue = (E) elementData[index]; |
381 |
|
elementData[index] = element; |
389 |
|
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
390 |
|
*/ |
391 |
|
public boolean add(E e) { |
392 |
< |
++modCount; |
393 |
< |
int s = size++; |
392 |
> |
modCount++; |
393 |
> |
int s = size; |
394 |
|
if (s >= elementData.length) |
395 |
|
growArray(s + 1); |
396 |
|
elementData[s] = e; |
397 |
+ |
size = s + 1; |
398 |
|
return true; |
399 |
|
} |
400 |
|
|
410 |
|
public void add(int index, E element) { |
411 |
|
int s = size; |
412 |
|
if (index > s || index < 0) |
413 |
< |
throw rangeException(index, s); |
414 |
< |
++modCount; |
400 |
< |
size = s + 1; |
413 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
414 |
> |
modCount++; |
415 |
|
if (s >= elementData.length) |
416 |
|
growArray(s + 1); |
417 |
< |
System.arraycopy(elementData, index, elementData, index + 1, |
418 |
< |
s - index); |
417 |
> |
System.arraycopy(elementData, index, |
418 |
> |
elementData, index + 1, s - index); |
419 |
|
elementData[index] = element; |
420 |
+ |
size = s + 1; |
421 |
|
} |
422 |
|
|
423 |
|
/** |
432 |
|
public E remove(int index) { |
433 |
|
int s = size - 1; |
434 |
|
if (index > s) |
435 |
< |
throw rangeException(index, size); |
421 |
< |
size = s; |
435 |
> |
throw new IndexOutOfBoundsException(ioobe(index)); |
436 |
|
modCount++; |
437 |
< |
Object oldValue = elementData[index]; |
437 |
> |
E oldValue = (E)elementData[index]; |
438 |
|
int numMoved = s - index; |
439 |
|
if (numMoved > 0) |
440 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
441 |
< |
numMoved); |
442 |
< |
elementData[s] = null; // forget removed element |
443 |
< |
return (E)oldValue; |
440 |
> |
System.arraycopy(elementData, index + 1, |
441 |
> |
elementData, index, numMoved); |
442 |
> |
elementData[s] = null; |
443 |
> |
size = s; |
444 |
> |
return oldValue; |
445 |
|
} |
446 |
|
|
447 |
|
/** |
540 |
|
*/ |
541 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
542 |
|
if (index > size || index < 0) |
543 |
< |
throw new IndexOutOfBoundsException( |
529 |
< |
"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 |
< |
|
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 |
|
* |
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; |
690 |
< |
return e; |
691 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
692 |
< |
} |
693 |
< |
} |
694 |
< |
} |
695 |
< |
// Prefer reporting CME if applicable on failures |
696 |
< |
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 |
|
} |
717 |
|
|
718 |
|
public E previous() { |
719 |
< |
if (expectedModCount == modCount) { |
719 |
> |
try { |
720 |
|
int i = cursor - 1; |
721 |
< |
if (i < size) { |
722 |
< |
try { |
723 |
< |
E e = (E)elementData[i]; |
724 |
< |
lastRet = i; |
725 |
< |
cursor = i; |
709 |
< |
return e; |
710 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
711 |
< |
} |
712 |
< |
} |
713 |
< |
} |
714 |
< |
if (expectedModCount == modCount) |
721 |
> |
E prev = get(i); |
722 |
> |
lastRet = i; |
723 |
> |
cursor = i; |
724 |
> |
return prev; |
725 |
> |
} catch (IndexOutOfBoundsException ex) { |
726 |
|
throw new NoSuchElementException(); |
727 |
< |
throw new ConcurrentModificationException(); |
727 |
> |
} finally { |
728 |
> |
if (modCount != expectedModCount) |
729 |
> |
throw new ConcurrentModificationException(); |
730 |
> |
} |
731 |
|
} |
732 |
|
|
733 |
|
public void remove() { |
734 |
|
if (lastRet < 0) |
735 |
|
throw new IllegalStateException(); |
736 |
< |
if (modCount != expectedModCount) |
736 |
> |
if (modCount != expectedModCount) |
737 |
|
throw new ConcurrentModificationException(); |
738 |
|
ArrayList.this.remove(lastRet); |
739 |
|
if (lastRet < cursor) |
745 |
|
public void set(E e) { |
746 |
|
if (lastRet < 0) |
747 |
|
throw new IllegalStateException(); |
748 |
< |
if (modCount != expectedModCount) |
748 |
> |
if (modCount != expectedModCount) |
749 |
|
throw new ConcurrentModificationException(); |
750 |
|
ArrayList.this.set(lastRet, e); |
751 |
|
expectedModCount = modCount; |
752 |
|
} |
753 |
|
|
754 |
|
public void add(E e) { |
755 |
< |
if (modCount != expectedModCount) |
755 |
> |
if (modCount != expectedModCount) |
756 |
|
throw new ConcurrentModificationException(); |
757 |
|
ArrayList.this.add(cursor++, e); |
758 |
|
lastRet = -1; |
759 |
|
expectedModCount = modCount; |
760 |
|
} |
761 |
|
} |
748 |
– |
|
762 |
|
} |