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.4 by jsr166, Sat Nov 26 03:12:10 2005 UTC vs.
Revision 1.21 by jsr166, Sun May 28 23:36:29 2006 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
8   package java.util;
9 import java.util.*; // for javadoc (till 6280605 is fixed)
9  
10   /**
11   * Resizable-array implementation of the <tt>List</tt> interface.  Implements
# Line 67 | Line 66 | import java.util.*; // for javadoc (till
66   * should be used only to detect bugs.</i><p>
67   *
68   * This class is a member of the
69 < * <a href="{@docRoot}/../guide/collections/index.html">
69 > * <a href="{@docRoot}/../technotes/guides/collections/index.html">
70   * Java Collections Framework</a>.
71   *
72   * @author  Josh Bloch
# Line 101 | Line 100 | public class ArrayList<E> extends Abstra
100      /**
101       * Constructs an empty list with the specified initial capacity.
102       *
103 <     * @param   initialCapacity   the initial capacity of the list
104 <     * @exception IllegalArgumentException if the specified initial capacity
105 <     *            is negative
103 >     * @param initialCapacity the initial capacity of the list
104 >     * @throws IllegalArgumentException if the specified initial capacity
105 >     *         is negative
106       */
107      public ArrayList(int initialCapacity) {
108          super();
# Line 123 | 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
127 <     * 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) {
131 <        int size = c.size();
132 <        // 10% for growth
133 <        int cap = ((size/10)+1)*11;
134 <        if (cap > 0) {
135 <            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);
131 >        elementData = c.toArray();
132 >        size = elementData.length;
133 >        // c.toArray might (incorrectly) not return Object[] (see 6260652)
134 >        if (elementData.getClass() != Object[].class)
135 >            elementData = Arrays.copyOf(elementData, size, Object[].class);
136      }
137  
138      private void initFromConcurrentlyMutating(Collection<? extends E> c) {
# Line 175 | Line 163 | public class ArrayList<E> extends Abstra
163       * necessary, to ensure that it can hold at least the number of elements
164       * specified by the minimum capacity argument.
165       *
166 <     * @param   minCapacity   the desired minimum capacity
166 >     * @param minCapacity the desired minimum capacity
167       */
168      public void ensureCapacity(int minCapacity) {
169          modCount++;
# Line 184 | Line 172 | public class ArrayList<E> extends Abstra
172      }
173  
174      /**
175 <     * Increase the capacity of the array.
176 <     * @param   minCapacity   the desired minimum capacity
175 >     * Increases the capacity of the array.
176 >     *
177 >     * @param minCapacity the desired minimum capacity
178       */
179      private void growArray(int minCapacity) {
180 +        if (minCapacity < 0) // overflow
181 +            throw new OutOfMemoryError();
182          int oldCapacity = elementData.length;
183          // Double size if small; else grow by 50%
184 <        int newCapacity = ((oldCapacity < 64)?
185 <                           (oldCapacity * 2):
186 <                           ((oldCapacity * 3)/2 + 1));
184 >        int newCapacity = ((oldCapacity < 64) ?
185 >                           ((oldCapacity + 1) * 2) :
186 >                           ((oldCapacity / 2) * 3));
187 >        if (newCapacity < 0) // overflow
188 >            newCapacity = Integer.MAX_VALUE;
189          if (newCapacity < minCapacity)
190              newCapacity = minCapacity;
191          elementData = Arrays.copyOf(elementData, newCapacity);
# Line 342 | Line 335 | public class ArrayList<E> extends Abstra
335      // Positional Access Operations
336  
337      /**
338 <     * Create and return an appropriate exception for indexing errors
338 >     * Throws an appropriate exception for indexing errors.
339       */
340 <    private static IndexOutOfBoundsException rangeException(int i, int s) {
341 <        return new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
340 >    private static void indexOutOfBounds(int i, int s) {
341 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
342      }
343  
351    // Positional Access Operations
352
344      /**
345       * Returns the element at the specified position in this list.
346       *
# Line 359 | Line 350 | public class ArrayList<E> extends Abstra
350       */
351      public E get(int index) {
352          if (index >= size)
353 <            throw rangeException(index, size);
354 <        return (E)elementData[index];
353 >            indexOutOfBounds(index, size);
354 >        return (E) elementData[index];
355      }
356  
357      /**
# Line 374 | Line 365 | public class ArrayList<E> extends Abstra
365       */
366      public E set(int index, E element) {
367          if (index >= size)
368 <             throw rangeException(index, size);
378 <
368 >            indexOutOfBounds(index, size);
369          E oldValue = (E) elementData[index];
370          elementData[index] = element;
371          return oldValue;
# Line 388 | Line 378 | public class ArrayList<E> extends Abstra
378       * @return <tt>true</tt> (as specified by {@link Collection#add})
379       */
380      public boolean add(E e) {
381 <        ++modCount;
382 <        int s = size++;
381 >        modCount++;
382 >        int s = size;
383          if (s >= elementData.length)
384              growArray(s + 1);
385          elementData[s] = e;
386 <        return true;
386 >        size = s + 1;
387 >        return true;
388      }
389  
390      /**
# Line 408 | Line 399 | public class ArrayList<E> extends Abstra
399      public void add(int index, E element) {
400          int s = size;
401          if (index > s || index < 0)
402 <            throw rangeException(index, s);
403 <        ++modCount;
413 <        size = s + 1;
402 >            indexOutOfBounds(index, s);
403 >        modCount++;
404          if (s >= elementData.length)
405              growArray(s + 1);
406 <        System.arraycopy(elementData, index, elementData, index + 1,
407 <                         s - index);
406 >        System.arraycopy(elementData, index,
407 >                         elementData, index + 1, s - index);
408          elementData[index] = element;
409 +        size = s + 1;
410      }
411  
412      /**
# Line 430 | Line 421 | public class ArrayList<E> extends Abstra
421      public E remove(int index) {
422          int s = size - 1;
423          if (index > s)
424 <            throw rangeException(index, size);
434 <        size = s;
424 >            indexOutOfBounds(index, size);
425          modCount++;
426 <        Object oldValue = elementData[index];
426 >        E oldValue = (E) elementData[index];
427          int numMoved = s - index;
428          if (numMoved > 0)
429 <            System.arraycopy(elementData, index+1, elementData, index,
430 <                             numMoved);
431 <        elementData[s] = null; // forget removed element
432 <        return (E)oldValue;
429 >            System.arraycopy(elementData, index + 1,
430 >                             elementData, index, numMoved);
431 >        elementData[s] = null;
432 >        size = s;
433 >        return oldValue;
434      }
435  
436      /**
# Line 538 | Line 529 | public class ArrayList<E> extends Abstra
529       */
530      public boolean addAll(int index, Collection<? extends E> c) {
531          if (index > size || index < 0)
532 <            throw new IndexOutOfBoundsException(
542 <                "Index: " + index + ", Size: " + size);
532 >            indexOutOfBounds(index, size);
533  
534          Object[] a = c.toArray();
535          int numNew = a.length;
# Line 601 | Line 591 | public class ArrayList<E> extends Abstra
591          for (int i=0; i<size; i++)
592              s.writeObject(elementData[i]);
593  
594 <        if (modCount != expectedModCount) {
594 >        if (expectedModCount != modCount) {
595              throw new ConcurrentModificationException();
596          }
597  
# Line 624 | Line 614 | public class ArrayList<E> extends Abstra
614          for (int i=0; i<size; i++)
615              a[i] = s.readObject();
616      }
627
628
629    /**
630     * Returns a list-iterator of the elements in this list (in proper
631     * sequence), starting at the specified position in the list.
632     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
633     *
634     * The list-iterator is <i>fail-fast</i>: if the list is structurally
635     * modified at any time after the Iterator is created, in any way except
636     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
637     * methods, the list-iterator will throw a
638     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
639     * concurrent modification, the iterator fails quickly and cleanly, rather
640     * than risking arbitrary, non-deterministic behavior at an undetermined
641     * time in the future.
642     *
643     * @param index index of the first element to be returned from the
644     *              list-iterator (by a call to <tt>next</tt>)
645     * @return a ListIterator of the elements in this list (in proper
646     *         sequence), starting at the specified position in the list
647     * @throws IndexOutOfBoundsException {@inheritDoc}
648     * @see List#listIterator(int)
649     */
650    public ListIterator<E> listIterator(int index) {
651        if (index < 0 || index > size)
652            throw new IndexOutOfBoundsException("Index: "+index);
653        return new ArrayListIterator(index);
654    }
655
656    /**
657     * Returns an iterator over the elements in this list in proper sequence.
658     *
659     * @return an iterator over the elements in this list in proper sequence
660     */
661    public Iterator<E> iterator() {
662        return new ArrayListIterator(0);
663    }
664
665    /**
666     * A streamlined version of AbstractList.Itr
667     */
668    final class ArrayListIterator implements ListIterator<E> {
669        int cursor;           // index of next element to return;
670        int lastRet;          // index of last element, or -1 if no such
671        int expectedModCount; // to check for CME
672
673        ArrayListIterator(int index) {
674            cursor = index;
675            lastRet = -1;
676            expectedModCount = modCount;
677        }
678
679        public boolean hasNext() {
680            return cursor < size;
681        }
682
683        public boolean hasPrevious() {
684            return cursor > 0;
685        }
686
687        public int nextIndex() {
688            return cursor;
689        }
690
691        public int previousIndex() {
692            return cursor - 1;
693        }
694
695        public E next() {
696            if (expectedModCount == modCount) {
697                int i = cursor;
698                if (i < size) {
699                    try {
700                        E e = (E)elementData[i];
701                        lastRet = i;
702                        cursor = i + 1;
703                        return e;
704                    } catch (IndexOutOfBoundsException fallthrough) {
705                    }
706                }
707            }
708            // Prefer reporting CME if applicable on failures
709            if (expectedModCount == modCount)
710                throw new NoSuchElementException();
711            throw new ConcurrentModificationException();
712        }
713
714        public E previous() {
715            if (expectedModCount == modCount) {
716                int i = cursor - 1;
717                if (i < size) {
718                    try {
719                        E e = (E)elementData[i];
720                        lastRet = i;
721                        cursor = i;
722                        return e;
723                    } catch (IndexOutOfBoundsException fallthrough) {
724                    }
725                }
726            }
727            if (expectedModCount == modCount)
728                throw new NoSuchElementException();
729            throw new ConcurrentModificationException();
730        }
731
732        public void remove() {
733            if (lastRet < 0)
734                throw new IllegalStateException();
735            if (modCount != expectedModCount)
736                throw new ConcurrentModificationException();
737            ArrayList.this.remove(lastRet);
738            if (lastRet < cursor)
739                cursor--;
740            lastRet = -1;
741            expectedModCount = modCount;
742        }
743
744        public void set(E e) {
745            if (lastRet < 0)
746                throw new IllegalStateException();
747            if (modCount != expectedModCount)
748                throw new ConcurrentModificationException();
749            ArrayList.this.set(lastRet, e);
750            expectedModCount = modCount;
751        }
752
753        public void add(E e) {
754            if (modCount != expectedModCount)
755                throw new ConcurrentModificationException();
756            ArrayList.this.add(cursor++, e);
757            lastRet = -1;
758            expectedModCount = modCount;
759        }
760    }
761
617   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines