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(); |
146 |
|
} |
147 |
|
initFromConcurrentlyMutating(c); |
148 |
|
} |
149 |
< |
|
149 |
> |
|
150 |
|
private void initFromConcurrentlyMutating(Collection<? extends E> c) { |
151 |
|
elementData = c.toArray(); |
152 |
|
size = elementData.length; |
154 |
|
if (elementData.getClass() != Object[].class) |
155 |
|
elementData = Arrays.copyOf(elementData, size, Object[].class); |
156 |
|
} |
157 |
< |
|
157 |
> |
|
158 |
|
private final static Object UNALLOCATED = new Object(); |
159 |
< |
|
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 |
179 |
< |
*/ |
180 |
< |
/** |
181 |
< |
* Increases the capacity of this <tt>ArrayList</tt> instance, if |
182 |
< |
* necessary, to ensure that it can hold at least the number of elements |
183 |
< |
* specified by the minimum capacity argument. |
184 |
< |
* |
185 |
< |
* @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) throw new OutOfMemoryError(); // int overflow |
193 |
|
int oldCapacity = elementData.length; |
194 |
|
// Double size if small; else grow by 50% |
195 |
< |
int newCapacity = ((oldCapacity < 64)? |
195 |
> |
int newCapacity = ((oldCapacity < 64)? |
196 |
|
(oldCapacity * 2): |
197 |
< |
((oldCapacity * 3)/2 + 1)); |
197 |
> |
((oldCapacity * 3)/2)); |
198 |
|
if (newCapacity < minCapacity) |
199 |
|
newCapacity = minCapacity; |
200 |
|
elementData = Arrays.copyOf(elementData, newCapacity); |
343 |
|
|
344 |
|
// Positional Access Operations |
345 |
|
|
346 |
< |
/** |
347 |
< |
* Create and return an appropriate exception for indexing errors |
346 |
> |
/** |
347 |
> |
* Throws an appropriate exception for indexing errors. |
348 |
|
*/ |
349 |
< |
private static IndexOutOfBoundsException rangeException(int i, int s) { |
350 |
< |
return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
349 |
> |
private static void rangeException(int i, int s) { |
350 |
> |
throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
351 |
|
} |
352 |
|
|
358 |
– |
// Positional Access Operations |
359 |
– |
|
353 |
|
/** |
354 |
|
* Returns the element at the specified position in this list. |
355 |
|
* |
359 |
|
*/ |
360 |
|
public E get(int index) { |
361 |
|
if (index >= size) |
362 |
< |
throw rangeException(index, size); |
362 |
> |
rangeException(index, size); |
363 |
|
return (E)elementData[index]; |
364 |
|
} |
365 |
|
|
374 |
|
*/ |
375 |
|
public E set(int index, E element) { |
376 |
|
if (index >= size) |
377 |
< |
throw rangeException(index, size); |
377 |
> |
rangeException(index, size); |
378 |
|
|
379 |
|
E oldValue = (E) elementData[index]; |
380 |
|
elementData[index] = element; |
388 |
|
* @return <tt>true</tt> (as specified by {@link Collection#add}) |
389 |
|
*/ |
390 |
|
public boolean add(E e) { |
391 |
< |
++modCount; |
392 |
< |
int s = size++; |
391 |
> |
modCount++; |
392 |
> |
int s = size; |
393 |
|
if (s >= elementData.length) |
394 |
|
growArray(s + 1); |
395 |
|
elementData[s] = e; |
396 |
+ |
size = s + 1; |
397 |
|
return true; |
398 |
|
} |
399 |
|
|
409 |
|
public void add(int index, E element) { |
410 |
|
int s = size; |
411 |
|
if (index > s || index < 0) |
412 |
< |
throw rangeException(index, s); |
413 |
< |
++modCount; |
420 |
< |
size = s + 1; |
412 |
> |
rangeException(index, s); |
413 |
> |
modCount++; |
414 |
|
if (s >= elementData.length) |
415 |
|
growArray(s + 1); |
416 |
< |
System.arraycopy(elementData, index, elementData, index + 1, |
417 |
< |
s - index); |
416 |
> |
System.arraycopy(elementData, index, |
417 |
> |
elementData, index + 1, s - index); |
418 |
|
elementData[index] = element; |
419 |
+ |
size = s + 1; |
420 |
|
} |
421 |
|
|
422 |
|
/** |
431 |
|
public E remove(int index) { |
432 |
|
int s = size - 1; |
433 |
|
if (index > s) |
434 |
< |
throw rangeException(index, size); |
441 |
< |
size = s; |
434 |
> |
rangeException(index, size); |
435 |
|
modCount++; |
436 |
< |
Object oldValue = elementData[index]; |
436 |
> |
E oldValue = (E)elementData[index]; |
437 |
|
int numMoved = s - index; |
438 |
|
if (numMoved > 0) |
439 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
440 |
< |
numMoved); |
441 |
< |
elementData[s] = null; // forget removed element |
442 |
< |
return (E)oldValue; |
439 |
> |
System.arraycopy(elementData, index + 1, |
440 |
> |
elementData, index, numMoved); |
441 |
> |
elementData[s] = null; |
442 |
> |
size = s; |
443 |
> |
return oldValue; |
444 |
|
} |
445 |
|
|
446 |
|
/** |
653 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
654 |
|
return new ArrayListIterator(index); |
655 |
|
} |
656 |
< |
|
656 |
> |
|
657 |
|
/** |
658 |
|
* Returns an iterator over the elements in this list in proper sequence. |
659 |
|
* |
664 |
|
} |
665 |
|
|
666 |
|
/** |
667 |
< |
* A streamlined version of AbstractList.Itr |
667 |
> |
* A streamlined version of AbstractList.Itr |
668 |
|
*/ |
669 |
|
final class ArrayListIterator implements ListIterator<E> { |
670 |
|
int cursor; // index of next element to return; |
702 |
|
lastRet = i; |
703 |
|
cursor = i + 1; |
704 |
|
return e; |
705 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
705 |
> |
} catch (IndexOutOfBoundsException fallthrough) { |
706 |
|
} |
707 |
|
} |
708 |
|
} |
721 |
|
lastRet = i; |
722 |
|
cursor = i; |
723 |
|
return e; |
724 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
724 |
> |
} catch (IndexOutOfBoundsException fallthrough) { |
725 |
|
} |
726 |
|
} |
727 |
|
} |
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; |