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.1 by dl, Fri Nov 25 13:27:05 2005 UTC vs.
Revision 1.5 by jsr166, Sat Nov 26 04:33:04 2005 UTC

# Line 101 | Line 101 | public class ArrayList<E> extends Abstra
101      /**
102       * Constructs an empty list with the specified initial capacity.
103       *
104 <     * @param   initialCapacity   the initial capacity of the list
105 <     * @exception IllegalArgumentException if the specified initial capacity
106 <     *            is negative
104 >     * @param initialCapacity the initial capacity of the list
105 >     * @throws IllegalArgumentException if the specified initial capacity
106 >     *         is negative
107       */
108      public ArrayList(int initialCapacity) {
109          super();
# Line 123 | Line 123 | public class ArrayList<E> extends Abstra
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
# Line 155 | Line 175 | public class ArrayList<E> extends Abstra
175       * necessary, to ensure that it can hold at least the number of elements
176       * specified by the minimum capacity argument.
177       *
178 <     * @param   minCapacity   the desired minimum capacity
159 <     */
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
178 >     * @param minCapacity the desired minimum capacity
179       */
180      public void ensureCapacity(int minCapacity) {
181          modCount++;
# Line 171 | Line 184 | public class ArrayList<E> extends Abstra
184      }
185  
186      /**
187 <     * Increase the capacity of the array.
188 <     * @param   minCapacity   the desired minimum capacity
187 >     * Increases the capacity of the array.
188 >     *
189 >     * @param minCapacity the desired minimum capacity
190       */
191      private void growArray(int minCapacity) {
192          int oldCapacity = elementData.length;
193          // Double size if small; else grow by 50%
194 <        int newCapacity = ((oldCapacity < 64)?
194 >        int newCapacity = ((oldCapacity < 64)?
195                             (oldCapacity * 2):
196                             ((oldCapacity * 3)/2 + 1));
197          if (newCapacity < minCapacity)
# Line 328 | Line 342 | public class ArrayList<E> extends Abstra
342  
343      // Positional Access Operations
344  
345 <    /**
346 <     * Create and return an appropriate exception for indexing errors
345 >    /**
346 >     * Create and return an appropriate exception for indexing errors
347       */
348      private static IndexOutOfBoundsException rangeException(int i, int s) {
349          return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
350      }
351  
338    // Positional Access Operations
339
352      /**
353       * Returns the element at the specified position in this list.
354       *
# Line 423 | Line 435 | public class ArrayList<E> extends Abstra
435          Object oldValue = elementData[index];
436          int numMoved = s - index;
437          if (numMoved > 0)
438 <            System.arraycopy(elementData, index+1, elementData, index,
438 >            System.arraycopy(elementData, index+1, elementData, index,
439                               numMoved);
440          elementData[s] = null; // forget removed element
441          return (E)oldValue;
# Line 639 | Line 651 | public class ArrayList<E> extends Abstra
651              throw new IndexOutOfBoundsException("Index: "+index);
652          return new ArrayListIterator(index);
653      }
654 <
654 >
655      /**
656       * Returns an iterator over the elements in this list in proper sequence.
657       *
# Line 650 | Line 662 | public class ArrayList<E> extends Abstra
662      }
663  
664      /**
665 <     * A streamlined version of AbstractList.Itr
665 >     * A streamlined version of AbstractList.Itr
666       */
667      final class ArrayListIterator implements ListIterator<E> {
668          int cursor;           // index of next element to return;
# Line 688 | Line 700 | public class ArrayList<E> extends Abstra
700                          lastRet = i;
701                          cursor = i + 1;
702                          return e;
703 <                    } catch (IndexOutOfBoundsException fallthrough) {
703 >                    } catch (IndexOutOfBoundsException fallthrough) {
704                      }
705                  }
706              }
# Line 707 | Line 719 | public class ArrayList<E> extends Abstra
719                          lastRet = i;
720                          cursor = i;
721                          return e;
722 <                    } catch (IndexOutOfBoundsException fallthrough) {
722 >                    } catch (IndexOutOfBoundsException fallthrough) {
723                      }
724                  }
725              }
# Line 719 | Line 731 | public class ArrayList<E> extends Abstra
731          public void remove() {
732              if (lastRet < 0)
733                  throw new IllegalStateException();
734 <            if (modCount != expectedModCount)
734 >            if (modCount != expectedModCount)
735                  throw new ConcurrentModificationException();
736              ArrayList.this.remove(lastRet);
737              if (lastRet < cursor)
# Line 731 | Line 743 | public class ArrayList<E> extends Abstra
743          public void set(E e) {
744              if (lastRet < 0)
745                  throw new IllegalStateException();
746 <            if (modCount != expectedModCount)
746 >            if (modCount != expectedModCount)
747                  throw new ConcurrentModificationException();
748              ArrayList.this.set(lastRet, e);
749              expectedModCount = modCount;
750          }
751  
752          public void add(E e) {
753 <            if (modCount != expectedModCount)
753 >            if (modCount != expectedModCount)
754                  throw new ConcurrentModificationException();
755              ArrayList.this.add(cursor++, e);
756              lastRet = -1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines