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.21 by jsr166, Sun May 28 23:36:29 2006 UTC

# Line 66 | Line 66 | package java.util;
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 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) {
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];
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);
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 188 | Line 177 | public class ArrayList<E> extends Abstra
177       * @param minCapacity the desired minimum capacity
178       */
179      private void growArray(int minCapacity) {
180 <        if (minCapacity < 0) // overflow
181 <            throw new OutOfMemoryError();
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 + 1) * 2):
184 >        int newCapacity = ((oldCapacity < 64) ?
185 >                           ((oldCapacity + 1) * 2) :
186                             ((oldCapacity / 2) * 3));
187          if (newCapacity < 0) // overflow
188              newCapacity = Integer.MAX_VALUE;
# Line 346 | Line 335 | public class ArrayList<E> extends Abstra
335      // Positional Access Operations
336  
337      /**
338 <     * Returns error message string for IndexOutOfBoundsExceptions
338 >     * Throws an appropriate exception for indexing errors.
339       */
340 <    private String ioobe(int index) {
341 <        return "Index: " + index + ", Size: " + size;
340 >    private static void indexOutOfBounds(int i, int s) {
341 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
342      }
343  
344      /**
# Line 361 | Line 350 | public class ArrayList<E> extends Abstra
350       */
351      public E get(int index) {
352          if (index >= size)
353 <            throw new IndexOutOfBoundsException(ioobe(index));
354 <        return (E)elementData[index];
353 >            indexOutOfBounds(index, size);
354 >        return (E) elementData[index];
355      }
356  
357      /**
# Line 376 | Line 365 | public class ArrayList<E> extends Abstra
365       */
366      public E set(int index, E element) {
367          if (index >= size)
368 <            throw new IndexOutOfBoundsException(ioobe(index));
380 <
368 >            indexOutOfBounds(index, size);
369          E oldValue = (E) elementData[index];
370          elementData[index] = element;
371          return oldValue;
# Line 395 | Line 383 | public class ArrayList<E> extends Abstra
383          if (s >= elementData.length)
384              growArray(s + 1);
385          elementData[s] = e;
386 <        size = s + 1;
387 <        return true;
386 >        size = s + 1;
387 >        return true;
388      }
389  
390      /**
# Line 411 | 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 new IndexOutOfBoundsException(ioobe(index));
402 >            indexOutOfBounds(index, s);
403          modCount++;
404          if (s >= elementData.length)
405              growArray(s + 1);
406          System.arraycopy(elementData, index,
407 <                         elementData, index + 1, s - index);
407 >                         elementData, index + 1, s - index);
408          elementData[index] = element;
409          size = s + 1;
410      }
# Line 433 | 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 new IndexOutOfBoundsException(ioobe(index));
424 >            indexOutOfBounds(index, size);
425          modCount++;
426 <        E oldValue = (E)elementData[index];
426 >        E oldValue = (E) elementData[index];
427          int numMoved = s - index;
428          if (numMoved > 0)
429              System.arraycopy(elementData, index + 1,
430 <                             elementData, index, numMoved);
430 >                             elementData, index, numMoved);
431          elementData[s] = null;
432 <        size = s;
432 >        size = s;
433          return oldValue;
434      }
435  
# Line 541 | 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(ioobe(index));
532 >            indexOutOfBounds(index, size);
533  
534          Object[] a = c.toArray();
535          int numNew = a.length;
# Line 626 | Line 614 | public class ArrayList<E> extends Abstra
614          for (int i=0; i<size; i++)
615              a[i] = s.readObject();
616      }
629
630
631    /**
632     * Returns a list-iterator of the elements in this list (in proper
633     * sequence), starting at the specified position in the list.
634     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
635     *
636     * The list-iterator is <i>fail-fast</i>: if the list is structurally
637     * modified at any time after the Iterator is created, in any way except
638     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
639     * methods, the list-iterator will throw a
640     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
641     * concurrent modification, the iterator fails quickly and cleanly, rather
642     * than risking arbitrary, non-deterministic behavior at an undetermined
643     * time in the future.
644     *
645     * @param index index of the first element to be returned from the
646     *              list-iterator (by a call to <tt>next</tt>)
647     * @return a ListIterator of the elements in this list (in proper
648     *         sequence), starting at the specified position in the list
649     * @throws IndexOutOfBoundsException {@inheritDoc}
650     * @see List#listIterator(int)
651     */
652    public ListIterator<E> listIterator(int index) {
653        if (index < 0 || index > size)
654            throw new IndexOutOfBoundsException(ioobe(index));
655        return new ArrayListIterator(index);
656    }
657
658    /**
659     * {@inheritDoc}
660     */
661    public ListIterator<E> listIterator() {
662        return new ArrayListIterator(0);
663    }
664
665    /**
666     * Returns an iterator over the elements in this list in proper sequence.
667     *
668     * @return an iterator over the elements in this list in proper sequence
669     */
670    public Iterator<E> iterator() {
671        return new ArrayListIterator(0);
672    }
673
674    /**
675     * A streamlined version of AbstractList.ListItr
676     */
677    final class ArrayListIterator implements ListIterator<E> {
678        int cursor;           // index of next element to return;
679        int lastRet;          // index of last element, or -1 if no such
680        int expectedModCount; // to check for CME
681
682        ArrayListIterator(int index) {
683            cursor = index;
684            lastRet = -1;
685            expectedModCount = modCount;
686        }
687
688        public boolean hasNext() {
689            return cursor != size;
690        }
691
692        public boolean hasPrevious() {
693            return cursor != 0;
694        }
695
696        public int nextIndex() {
697            return cursor;
698        }
699
700        public int previousIndex() {
701            return cursor - 1;
702        }
703
704        public E next() {
705            try {
706                int i = cursor;
707                E next = get(i);
708                lastRet = i;
709                cursor = i + 1;
710                return next;
711            } catch (IndexOutOfBoundsException ex) {
712                throw new NoSuchElementException();
713            } finally {
714                if (expectedModCount != modCount)
715                    throw new ConcurrentModificationException();
716            }
717        }
718
719        public E previous() {
720            try {
721                int i = cursor - 1;
722                E prev = get(i);
723                lastRet = i;
724                cursor = i;
725                return prev;
726            } catch (IndexOutOfBoundsException ex) {
727                throw new NoSuchElementException();
728            } finally {
729                if (expectedModCount != modCount)
730                    throw new ConcurrentModificationException();
731            }
732        }
733
734        public void remove() {
735            if (lastRet < 0)
736                throw new IllegalStateException();
737            if (expectedModCount != modCount)
738                throw new ConcurrentModificationException();
739            ArrayList.this.remove(lastRet);
740            if (lastRet < cursor)
741                cursor--;
742            lastRet = -1;
743            expectedModCount = modCount;
744        }
745
746        public void set(E e) {
747            if (lastRet < 0)
748                throw new IllegalStateException();
749            if (expectedModCount != modCount)
750                throw new ConcurrentModificationException();
751            ArrayList.this.set(lastRet, e);
752            expectedModCount = modCount;
753        }
754
755        public void add(E e) {
756            if (expectedModCount != modCount)
757                throw new ConcurrentModificationException();
758            try {
759                ArrayList.this.add(cursor++, e);
760                lastRet = -1;
761                expectedModCount = modCount;
762            } catch (IndexOutOfBoundsException ex) {
763                throw new ConcurrentModificationException();
764            }
765        }
766    }
617   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines