1 |
|
/* |
2 |
< |
* %W% %E% |
2 |
> |
* @(#)ArrayList.java 1.56 06/03/14 |
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 |
71 |
|
* |
72 |
|
* @author Josh Bloch |
73 |
|
* @author Neal Gafter |
74 |
< |
* @version %I%, %G% |
74 |
> |
* @version 1.56, 03/14/06 |
75 |
|
* @see Collection |
76 |
|
* @see List |
77 |
|
* @see LinkedList |
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(); |
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. |
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 |
< |
Object[] a = c.toArray(); |
132 |
< |
// If c.toArray incorrectly doesn't return Object[], copy it. |
133 |
< |
if (a.getClass() != Object[].class) |
134 |
< |
a = Arrays.copyOf(a, a.length, Object[].class); |
135 |
< |
elementData = a; |
136 |
< |
size = a.length; |
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) { |
139 |
> |
elementData = c.toArray(); |
140 |
> |
size = elementData.length; |
141 |
> |
// c.toArray might (incorrectly) not return Object[] (see 6260652) |
142 |
> |
if (elementData.getClass() != Object[].class) |
143 |
> |
elementData = Arrays.copyOf(elementData, size, Object[].class); |
144 |
|
} |
145 |
|
|
146 |
+ |
private final static Object UNALLOCATED = new Object(); |
147 |
+ |
|
148 |
|
/** |
149 |
|
* Trims the capacity of this <tt>ArrayList</tt> instance to be the |
150 |
|
* list's current size. An application can use this operation to minimize |
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 |
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 |
166 |
> |
* @param minCapacity the desired minimum capacity |
167 |
|
*/ |
168 |
|
public void ensureCapacity(int minCapacity) { |
169 |
|
modCount++; |
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); |
334 |
|
|
335 |
|
// Positional Access Operations |
336 |
|
|
337 |
< |
/** |
338 |
< |
* Create and return an appropriate exception for indexing errors |
337 |
> |
/** |
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 |
|
|
338 |
– |
// Positional Access Operations |
339 |
– |
|
344 |
|
/** |
345 |
|
* Returns the element at the specified position in this list. |
346 |
|
* |
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 |
|
/** |
365 |
|
*/ |
366 |
|
public E set(int index, E element) { |
367 |
|
if (index >= size) |
368 |
< |
throw rangeException(index, size); |
365 |
< |
|
368 |
> |
indexOutOfBounds(index, size); |
369 |
|
E oldValue = (E) elementData[index]; |
370 |
|
elementData[index] = element; |
371 |
|
return oldValue; |
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 |
|
/** |
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; |
400 |
< |
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 |
|
/** |
421 |
|
public E remove(int index) { |
422 |
|
int s = size - 1; |
423 |
|
if (index > s) |
424 |
< |
throw rangeException(index, size); |
421 |
< |
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 |
|
/** |
529 |
|
*/ |
530 |
|
public boolean addAll(int index, Collection<? extends E> c) { |
531 |
|
if (index > size || index < 0) |
532 |
< |
throw new IndexOutOfBoundsException( |
529 |
< |
"Index: " + index + ", Size: " + size); |
532 |
> |
indexOutOfBounds(index, size); |
533 |
|
|
534 |
|
Object[] a = c.toArray(); |
535 |
|
int numNew = a.length; |
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 |
|
|
614 |
|
for (int i=0; i<size; i++) |
615 |
|
a[i] = s.readObject(); |
616 |
|
} |
614 |
– |
|
615 |
– |
|
616 |
– |
/** |
617 |
– |
* Returns a list-iterator of the elements in this list (in proper |
618 |
– |
* sequence), starting at the specified position in the list. |
619 |
– |
* Obeys the general contract of <tt>List.listIterator(int)</tt>.<p> |
620 |
– |
* |
621 |
– |
* The list-iterator is <i>fail-fast</i>: if the list is structurally |
622 |
– |
* modified at any time after the Iterator is created, in any way except |
623 |
– |
* through the list-iterator's own <tt>remove</tt> or <tt>add</tt> |
624 |
– |
* methods, the list-iterator will throw a |
625 |
– |
* <tt>ConcurrentModificationException</tt>. Thus, in the face of |
626 |
– |
* concurrent modification, the iterator fails quickly and cleanly, rather |
627 |
– |
* than risking arbitrary, non-deterministic behavior at an undetermined |
628 |
– |
* time in the future. |
629 |
– |
* |
630 |
– |
* @param index index of the first element to be returned from the |
631 |
– |
* list-iterator (by a call to <tt>next</tt>) |
632 |
– |
* @return a ListIterator of the elements in this list (in proper |
633 |
– |
* sequence), starting at the specified position in the list |
634 |
– |
* @throws IndexOutOfBoundsException {@inheritDoc} |
635 |
– |
* @see List#listIterator(int) |
636 |
– |
*/ |
637 |
– |
public ListIterator<E> listIterator(int index) { |
638 |
– |
if (index < 0 || index > size) |
639 |
– |
throw new IndexOutOfBoundsException("Index: "+index); |
640 |
– |
return new ArrayListIterator(index); |
641 |
– |
} |
642 |
– |
|
643 |
– |
/** |
644 |
– |
* Returns an iterator over the elements in this list in proper sequence. |
645 |
– |
* |
646 |
– |
* @return an iterator over the elements in this list in proper sequence |
647 |
– |
*/ |
648 |
– |
public Iterator<E> iterator() { |
649 |
– |
return new ArrayListIterator(0); |
650 |
– |
} |
651 |
– |
|
652 |
– |
/** |
653 |
– |
* A streamlined version of AbstractList.Itr |
654 |
– |
*/ |
655 |
– |
final class ArrayListIterator implements ListIterator<E> { |
656 |
– |
int cursor; // index of next element to return; |
657 |
– |
int lastRet; // index of last element, or -1 if no such |
658 |
– |
int expectedModCount; // to check for CME |
659 |
– |
|
660 |
– |
ArrayListIterator(int index) { |
661 |
– |
cursor = index; |
662 |
– |
lastRet = -1; |
663 |
– |
expectedModCount = modCount; |
664 |
– |
} |
665 |
– |
|
666 |
– |
public boolean hasNext() { |
667 |
– |
return cursor < size; |
668 |
– |
} |
669 |
– |
|
670 |
– |
public boolean hasPrevious() { |
671 |
– |
return cursor > 0; |
672 |
– |
} |
673 |
– |
|
674 |
– |
public int nextIndex() { |
675 |
– |
return cursor; |
676 |
– |
} |
677 |
– |
|
678 |
– |
public int previousIndex() { |
679 |
– |
return cursor - 1; |
680 |
– |
} |
681 |
– |
|
682 |
– |
public E next() { |
683 |
– |
if (expectedModCount == modCount) { |
684 |
– |
int i = cursor; |
685 |
– |
if (i < size) { |
686 |
– |
try { |
687 |
– |
E e = (E)elementData[i]; |
688 |
– |
lastRet = i; |
689 |
– |
cursor = i + 1; |
690 |
– |
return e; |
691 |
– |
} catch (IndexOutOfBoundsException fallthrough) { |
692 |
– |
} |
693 |
– |
} |
694 |
– |
} |
695 |
– |
// Prefer reporting CME if applicable on failures |
696 |
– |
if (expectedModCount == modCount) |
697 |
– |
throw new NoSuchElementException(); |
698 |
– |
throw new ConcurrentModificationException(); |
699 |
– |
} |
700 |
– |
|
701 |
– |
public E previous() { |
702 |
– |
if (expectedModCount == modCount) { |
703 |
– |
int i = cursor - 1; |
704 |
– |
if (i < size) { |
705 |
– |
try { |
706 |
– |
E e = (E)elementData[i]; |
707 |
– |
lastRet = i; |
708 |
– |
cursor = i; |
709 |
– |
return e; |
710 |
– |
} catch (IndexOutOfBoundsException fallthrough) { |
711 |
– |
} |
712 |
– |
} |
713 |
– |
} |
714 |
– |
if (expectedModCount == modCount) |
715 |
– |
throw new NoSuchElementException(); |
716 |
– |
throw new ConcurrentModificationException(); |
717 |
– |
} |
718 |
– |
|
719 |
– |
public void remove() { |
720 |
– |
if (lastRet < 0) |
721 |
– |
throw new IllegalStateException(); |
722 |
– |
if (modCount != expectedModCount) |
723 |
– |
throw new ConcurrentModificationException(); |
724 |
– |
ArrayList.this.remove(lastRet); |
725 |
– |
if (lastRet < cursor) |
726 |
– |
cursor--; |
727 |
– |
lastRet = -1; |
728 |
– |
expectedModCount = modCount; |
729 |
– |
} |
730 |
– |
|
731 |
– |
public void set(E e) { |
732 |
– |
if (lastRet < 0) |
733 |
– |
throw new IllegalStateException(); |
734 |
– |
if (modCount != expectedModCount) |
735 |
– |
throw new ConcurrentModificationException(); |
736 |
– |
ArrayList.this.set(lastRet, e); |
737 |
– |
expectedModCount = modCount; |
738 |
– |
} |
739 |
– |
|
740 |
– |
public void add(E e) { |
741 |
– |
if (modCount != expectedModCount) |
742 |
– |
throw new ConcurrentModificationException(); |
743 |
– |
ArrayList.this.add(cursor++, e); |
744 |
– |
lastRet = -1; |
745 |
– |
expectedModCount = modCount; |
746 |
– |
} |
747 |
– |
} |
748 |
– |
|
617 |
|
} |