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 |
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 |
|
int oldCapacity = elementData.length; |
193 |
|
// Double size if small; else grow by 50% |
194 |
< |
int newCapacity = ((oldCapacity < 64)? |
194 |
> |
int newCapacity = ((oldCapacity < 64)? |
195 |
|
(oldCapacity * 2): |
196 |
|
((oldCapacity * 3)/2 + 1)); |
197 |
|
if (newCapacity < minCapacity) |
342 |
|
|
343 |
|
// Positional Access Operations |
344 |
|
|
345 |
< |
/** |
346 |
< |
* Create and return an appropriate exception for indexing errors |
345 |
> |
/** |
346 |
> |
* Create and return an appropriate exception for indexing errors |
347 |
|
*/ |
348 |
|
private static IndexOutOfBoundsException rangeException(int i, int s) { |
349 |
|
return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s); |
350 |
|
} |
351 |
|
|
351 |
– |
// Positional Access Operations |
352 |
– |
|
352 |
|
/** |
353 |
|
* Returns the element at the specified position in this list. |
354 |
|
* |
435 |
|
Object oldValue = elementData[index]; |
436 |
|
int numMoved = s - index; |
437 |
|
if (numMoved > 0) |
438 |
< |
System.arraycopy(elementData, index+1, elementData, index, |
438 |
> |
System.arraycopy(elementData, index+1, elementData, index, |
439 |
|
numMoved); |
440 |
|
elementData[s] = null; // forget removed element |
441 |
|
return (E)oldValue; |
651 |
|
throw new IndexOutOfBoundsException("Index: "+index); |
652 |
|
return new ArrayListIterator(index); |
653 |
|
} |
654 |
< |
|
654 |
> |
|
655 |
|
/** |
656 |
|
* Returns an iterator over the elements in this list in proper sequence. |
657 |
|
* |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* A streamlined version of AbstractList.Itr |
665 |
> |
* A streamlined version of AbstractList.Itr |
666 |
|
*/ |
667 |
|
final class ArrayListIterator implements ListIterator<E> { |
668 |
|
int cursor; // index of next element to return; |
700 |
|
lastRet = i; |
701 |
|
cursor = i + 1; |
702 |
|
return e; |
703 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
703 |
> |
} catch (IndexOutOfBoundsException fallthrough) { |
704 |
|
} |
705 |
|
} |
706 |
|
} |
719 |
|
lastRet = i; |
720 |
|
cursor = i; |
721 |
|
return e; |
722 |
< |
} catch (IndexOutOfBoundsException fallthrough) { |
722 |
> |
} catch (IndexOutOfBoundsException fallthrough) { |
723 |
|
} |
724 |
|
} |
725 |
|
} |
731 |
|
public void remove() { |
732 |
|
if (lastRet < 0) |
733 |
|
throw new IllegalStateException(); |
734 |
< |
if (modCount != expectedModCount) |
734 |
> |
if (modCount != expectedModCount) |
735 |
|
throw new ConcurrentModificationException(); |
736 |
|
ArrayList.this.remove(lastRet); |
737 |
|
if (lastRet < cursor) |
743 |
|
public void set(E e) { |
744 |
|
if (lastRet < 0) |
745 |
|
throw new IllegalStateException(); |
746 |
< |
if (modCount != expectedModCount) |
746 |
> |
if (modCount != expectedModCount) |
747 |
|
throw new ConcurrentModificationException(); |
748 |
|
ArrayList.this.set(lastRet, e); |
749 |
|
expectedModCount = modCount; |
750 |
|
} |
751 |
|
|
752 |
|
public void add(E e) { |
753 |
< |
if (modCount != expectedModCount) |
753 |
> |
if (modCount != expectedModCount) |
754 |
|
throw new ConcurrentModificationException(); |
755 |
|
ArrayList.this.add(cursor++, e); |
756 |
|
lastRet = -1; |