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.18 by jsr166, Sun Mar 19 17:40:40 2006 UTC vs.
Revision 1.23 by jsr166, Sun Jan 7 07:38:27 2007 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
# 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 170 | Line 170 | public class ArrayList<E> extends Abstra
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      /**
# Line 325 | Line 325 | public class ArrayList<E> extends Abstra
325      // Positional Access Operations
326  
327      /**
328 <     * Returns error message string for IndexOutOfBoundsExceptions
328 >     * Throws an appropriate exception for indexing errors.
329       */
330 <    private String ioobe(int index) {
331 <        return "Index: " + index + ", Size: " + size;
330 >    private static void indexOutOfBounds(int i, int s) {
331 >        throw new IndexOutOfBoundsException("Index: " + i + ", Size: " + s);
332      }
333  
334      /**
# Line 340 | Line 340 | public class ArrayList<E> extends Abstra
340       */
341      public E get(int index) {
342          if (index >= size)
343 <            throw new IndexOutOfBoundsException(ioobe(index));
344 <        return (E)elementData[index];
343 >            indexOutOfBounds(index, size);
344 >        return (E) elementData[index];
345      }
346  
347      /**
# Line 355 | Line 355 | public class ArrayList<E> extends Abstra
355       */
356      public E set(int index, E element) {
357          if (index >= size)
358 <            throw new IndexOutOfBoundsException(ioobe(index));
359 <
358 >            indexOutOfBounds(index, size);
359          E oldValue = (E) elementData[index];
360          elementData[index] = element;
361          return oldValue;
# Line 374 | Line 373 | public class ArrayList<E> extends Abstra
373          if (s >= elementData.length)
374              growArray(s + 1);
375          elementData[s] = e;
376 <        size = s + 1;
377 <        return true;
376 >        size = s + 1;
377 >        return true;
378      }
379  
380      /**
# Line 390 | Line 389 | public class ArrayList<E> extends Abstra
389      public void add(int index, E element) {
390          int s = size;
391          if (index > s || index < 0)
392 <            throw new IndexOutOfBoundsException(ioobe(index));
392 >            indexOutOfBounds(index, s);
393          modCount++;
394          if (s >= elementData.length)
395              growArray(s + 1);
396          System.arraycopy(elementData, index,
397 <                         elementData, index + 1, s - index);
397 >                         elementData, index + 1, s - index);
398          elementData[index] = element;
399          size = s + 1;
400      }
# Line 412 | Line 411 | public class ArrayList<E> extends Abstra
411      public E remove(int index) {
412          int s = size - 1;
413          if (index > s)
414 <            throw new IndexOutOfBoundsException(ioobe(index));
414 >            indexOutOfBounds(index, size);
415          modCount++;
416 <        E oldValue = (E)elementData[index];
416 >        E oldValue = (E) elementData[index];
417          int numMoved = s - index;
418          if (numMoved > 0)
419              System.arraycopy(elementData, index + 1,
420 <                             elementData, index, numMoved);
420 >                             elementData, index, numMoved);
421          elementData[s] = null;
422 <        size = s;
422 >        size = s;
423          return oldValue;
424      }
425  
# Line 520 | Line 519 | public class ArrayList<E> extends Abstra
519       */
520      public boolean addAll(int index, Collection<? extends E> c) {
521          if (index > size || index < 0)
522 <            throw new IndexOutOfBoundsException(ioobe(index));
522 >            indexOutOfBounds(index, size);
523  
524          Object[] a = c.toArray();
525          int numNew = a.length;
# Line 605 | Line 604 | public class ArrayList<E> extends Abstra
604          for (int i=0; i<size; i++)
605              a[i] = s.readObject();
606      }
608
609
610    /**
611     * Returns a list-iterator of the elements in this list (in proper
612     * sequence), starting at the specified position in the list.
613     * Obeys the general contract of <tt>List.listIterator(int)</tt>.<p>
614     *
615     * The list-iterator is <i>fail-fast</i>: if the list is structurally
616     * modified at any time after the Iterator is created, in any way except
617     * through the list-iterator's own <tt>remove</tt> or <tt>add</tt>
618     * methods, the list-iterator will throw a
619     * <tt>ConcurrentModificationException</tt>.  Thus, in the face of
620     * concurrent modification, the iterator fails quickly and cleanly, rather
621     * than risking arbitrary, non-deterministic behavior at an undetermined
622     * time in the future.
623     *
624     * @param index index of the first element to be returned from the
625     *              list-iterator (by a call to <tt>next</tt>)
626     * @return a ListIterator of the elements in this list (in proper
627     *         sequence), starting at the specified position in the list
628     * @throws IndexOutOfBoundsException {@inheritDoc}
629     * @see List#listIterator(int)
630     */
631    public ListIterator<E> listIterator(int index) {
632        if (index < 0 || index > size)
633            throw new IndexOutOfBoundsException(ioobe(index));
634        return new ArrayListIterator(index);
635    }
636
637    /**
638     * {@inheritDoc}
639     */
640    public ListIterator<E> listIterator() {
641        return new ArrayListIterator(0);
642    }
643
644    /**
645     * Returns an iterator over the elements in this list in proper sequence.
646     *
647     * @return an iterator over the elements in this list in proper sequence
648     */
649    public Iterator<E> iterator() {
650        return new ArrayListIterator(0);
651    }
652
653    /**
654     * A streamlined version of AbstractList.ListItr
655     */
656    final class ArrayListIterator implements ListIterator<E> {
657        int cursor;           // index of next element to return;
658        int lastRet;          // index of last element, or -1 if no such
659        int expectedModCount; // to check for CME
660
661        ArrayListIterator(int index) {
662            cursor = index;
663            lastRet = -1;
664            expectedModCount = modCount;
665        }
666
667        public boolean hasNext() {
668            return cursor != size;
669        }
670
671        public boolean hasPrevious() {
672            return cursor != 0;
673        }
674
675        public int nextIndex() {
676            return cursor;
677        }
678
679        public int previousIndex() {
680            return cursor - 1;
681        }
682
683        public E next() {
684            try {
685                int i = cursor;
686                E next = get(i);
687                lastRet = i;
688                cursor = i + 1;
689                return next;
690            } catch (IndexOutOfBoundsException ex) {
691                throw new NoSuchElementException();
692            } finally {
693                if (expectedModCount != modCount)
694                    throw new ConcurrentModificationException();
695            }
696        }
697
698        public E previous() {
699            try {
700                int i = cursor - 1;
701                E prev = get(i);
702                lastRet = i;
703                cursor = i;
704                return prev;
705            } catch (IndexOutOfBoundsException ex) {
706                throw new NoSuchElementException();
707            } finally {
708                if (expectedModCount != modCount)
709                    throw new ConcurrentModificationException();
710            }
711        }
712
713        public void remove() {
714            if (lastRet < 0)
715                throw new IllegalStateException();
716            if (expectedModCount != modCount)
717                throw new ConcurrentModificationException();
718            ArrayList.this.remove(lastRet);
719            if (lastRet < cursor)
720                cursor--;
721            lastRet = -1;
722            expectedModCount = modCount;
723        }
724
725        public void set(E e) {
726            if (lastRet < 0)
727                throw new IllegalStateException();
728            if (expectedModCount != modCount)
729                throw new ConcurrentModificationException();
730            ArrayList.this.set(lastRet, e);
731            expectedModCount = modCount;
732        }
733
734        public void add(E e) {
735            if (expectedModCount != modCount)
736                throw new ConcurrentModificationException();
737            try {
738                ArrayList.this.add(cursor++, e);
739                lastRet = -1;
740                expectedModCount = modCount;
741            } catch (IndexOutOfBoundsException ex) {
742                throw new ConcurrentModificationException();
743            }
744        }
745    }
607   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines