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 |
< |
|
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 |