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