ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayList.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayList.java (file contents):
Revision 1.16 by jsr166, Tue Feb 7 20:54:24 2006 UTC vs.
Revision 1.18 by jsr166, Sun Mar 19 17:40:40 2006 UTC

# Line 122 | Line 122 | public class ArrayList<E> extends Abstra
122      /**
123       * Constructs a list containing the elements of the specified
124       * collection, in the order they are returned by the collection's
125 <     * iterator.  The <tt>ArrayList</tt> instance has an initial capacity of
126 <     * 110% the size of the specified collection.
125 >     * iterator.
126       *
127       * @param c the collection whose elements are to be placed into this list
128       * @throws NullPointerException if the specified collection is null
129       */
130      public ArrayList(Collection<? extends E> c) {
132        int size = c.size();
133        // 10% for growth
134        int cap = ((size/10)+1)*11;
135        if (cap > 0) {
136            Object[] a = new Object[cap];
137            a[size] = a[size+1] = UNALLOCATED;
138            Object[] b = c.toArray(a);
139            if (b[size] == null && b[size+1] == UNALLOCATED) {
140                b[size+1] = null;
141                elementData = b;
142                this.size = size;
143                return;
144            }
145        }
146        initFromConcurrentlyMutating(c);
147    }
148
149    private void initFromConcurrentlyMutating(Collection<? extends E> c) {
131          elementData = c.toArray();
132          size = elementData.length;
133          // c.toArray might (incorrectly) not return Object[] (see 6260652)
# Line 154 | Line 135 | public class ArrayList<E> extends Abstra
135              elementData = Arrays.copyOf(elementData, size, Object[].class);
136      }
137  
157    private final static Object UNALLOCATED = new Object();
158
138      /**
139       * Trims the capacity of this <tt>ArrayList</tt> instance to be the
140       * list's current size.  An application can use this operation to minimize
# Line 188 | Line 167 | public class ArrayList<E> extends Abstra
167       * @param minCapacity the desired minimum capacity
168       */
169      private void growArray(int minCapacity) {
170 <        if (minCapacity < 0) // overflow
171 <            throw new OutOfMemoryError();
170 >        if (minCapacity < 0) // overflow
171 >            throw new OutOfMemoryError();
172          int oldCapacity = elementData.length;
173 <        // Double size if small; else grow by 50%
174 <        int newCapacity = ((oldCapacity < 64)?
175 <                           ((oldCapacity + 1) * 2):
176 <                           ((oldCapacity / 2) * 3));
177 <        if (newCapacity < 0) // overflow
178 <            newCapacity = Integer.MAX_VALUE;
179 <        if (newCapacity < minCapacity)
180 <            newCapacity = minCapacity;
181 <        elementData = Arrays.copyOf(elementData, newCapacity);
173 >        // Double size if small; else grow by 50%
174 >        int newCapacity = ((oldCapacity < 64) ?
175 >                           ((oldCapacity + 1) * 2) :
176 >                           ((oldCapacity / 2) * 3));
177 >        if (newCapacity < 0) // overflow
178 >            newCapacity = Integer.MAX_VALUE;
179 >        if (newCapacity < minCapacity)
180 >            newCapacity = minCapacity;
181 >        elementData = Arrays.copyOf(elementData, newCapacity);
182      }
183  
184      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines