--- jsr166/src/main/java/util/ArrayList.java 2005/11/25 13:27:05 1.1 +++ jsr166/src/main/java/util/ArrayList.java 2005/11/25 13:34:29 1.2 @@ -123,20 +123,40 @@ public class ArrayList extends Abstra /** * Constructs a list containing the elements of the specified * collection, in the order they are returned by the collection's - * iterator. + * iterator. The ArrayList instance has an initial capacity of + * 110% the size of the specified collection. * * @param c the collection whose elements are to be placed into this list * @throws NullPointerException if the specified collection is null */ public ArrayList(Collection c) { - Object[] a = c.toArray(); - // If c.toArray incorrectly doesn't return Object[], copy it. - if (a.getClass() != Object[].class) - a = Arrays.copyOf(a, a.length, Object[].class); - elementData = a; - size = a.length; + int size = c.size(); + // 10% for growth + int cap = ((size/10)+1)*11; + if (cap > 0) { + Object[] a = new Object[cap]; + a[size] = a[size+1] = UNALLOCATED; + Object[] b = c.toArray(a); + if (b[size] == null && b[size+1] == UNALLOCATED) { + b[size+1] = null; + elementData = b; + this.size = size; + return; + } + } + initFromConcurrentlyMutating(c); } - + + private void initFromConcurrentlyMutating(Collection c) { + elementData = c.toArray(); + size = elementData.length; + // c.toArray might (incorrectly) not return Object[] (see 6260652) + if (elementData.getClass() != Object[].class) + elementData = Arrays.copyOf(elementData, size, Object[].class); + } + + private final static Object UNALLOCATED = new Object(); + /** * Trims the capacity of this ArrayList instance to be the * list's current size. An application can use this operation to minimize