104 |
|
Object[] r = new Object[size()]; |
105 |
|
int i = 0; |
106 |
|
Iterator it = iterator(); |
107 |
< |
while (i < r.length && it.hasNext()) |
107 |
> |
while (i < r.length && it.hasNext()) |
108 |
|
r[i++] = it.next(); |
109 |
|
// Trim if overallocated; expand if underallocated |
110 |
|
if (i < r.length || it.hasNext()) |
134 |
|
.newInstance(a.getClass().getComponentType(), size); |
135 |
|
int i = 0; |
136 |
|
Iterator it = iterator(); |
137 |
< |
while (i < r.length && it.hasNext()) |
137 |
> |
while (i < r.length && it.hasNext()) |
138 |
|
r[i++] = (T)it.next(); |
139 |
|
// Trim if overallocated; expand if underallocated |
140 |
|
if (it.hasNext() || (r != a && i < r.length)) |
141 |
|
return resizeAndFinishToArray(r, i, it); |
142 |
< |
if (i < r.length) |
142 |
> |
if (i < r.length) |
143 |
|
r[i] = null; // null-terminate if provided array is too big |
144 |
|
return r; |
145 |
|
} |
154 |
|
* @return larger array containing same elements |
155 |
|
*/ |
156 |
|
private static <T> T[] resizeAndFinishToArray(T[] r, int i, Iterator it) { |
157 |
< |
while (it.hasNext()) { |
157 |
> |
while (it.hasNext()) { |
158 |
|
int cap = r.length; |
159 |
|
if (i < cap) |
160 |
|
r[i++] = (T)it.next(); |
167 |
|
throw new OutOfMemoryError("Required array size too large"); |
168 |
|
} |
169 |
|
// trim if overallocated |
170 |
< |
return i == r.length? r : Arrays.copyOf(r, i); |
170 |
> |
return i == r.length? r : Arrays.copyOf(r, i); |
171 |
|
} |
172 |
|
|
173 |
|
// Modification Operations |