Fix a bug that broke -freorder-functions
[official-gcc.git] / libjava / classpath / java / util / Vector.java
blob44370a1e4dadc10f7899081392ad4db58884fde5
1 /* Vector.java -- Class that provides growable arrays.
2 Copyright (C) 1998, 1999, 2000, 2001, 2004, 2005, 2006,
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.util;
42 import java.io.IOException;
43 import java.io.ObjectOutputStream;
44 import java.io.Serializable;
45 import java.lang.reflect.Array;
47 /**
48 * The <code>Vector</code> classes implements growable arrays of Objects.
49 * You can access elements in a Vector with an index, just as you
50 * can in a built in array, but Vectors can grow and shrink to accommodate
51 * more or fewer objects.<p>
53 * Vectors try to mantain efficiency in growing by having a
54 * <code>capacityIncrement</code> that can be specified at instantiation.
55 * When a Vector can no longer hold a new Object, it grows by the amount
56 * in <code>capacityIncrement</code>. If this value is 0, the vector doubles in
57 * size.<p>
59 * Vector implements the JDK 1.2 List interface, and is therefore a fully
60 * compliant Collection object. The iterators are fail-fast - if external
61 * code structurally modifies the vector, any operation on the iterator will
62 * then throw a {@link ConcurrentModificationException}. The Vector class is
63 * fully synchronized, but the iterators are not. So, when iterating over a
64 * vector, be sure to synchronize on the vector itself. If you don't want the
65 * expense of synchronization, use ArrayList instead. On the other hand, the
66 * Enumeration of elements() is not thread-safe, nor is it fail-fast; so it
67 * can lead to undefined behavior even in a single thread if you modify the
68 * vector during iteration.<p>
70 * Note: Some methods, especially those specified by List, specify throwing
71 * {@link IndexOutOfBoundsException}, but it is easier to implement by
72 * throwing the subclass {@link ArrayIndexOutOfBoundsException}. Others
73 * directly specify this subclass.
75 * @author Scott G. Miller
76 * @author Bryce McKinlay
77 * @author Eric Blake (ebb9@email.byu.edu)
78 * @see Collection
79 * @see List
80 * @see ArrayList
81 * @see LinkedList
82 * @since 1.0
83 * @status updated to 1.4
85 public class Vector<T> extends AbstractList<T>
86 implements List<T>, RandomAccess, Cloneable, Serializable
88 /**
89 * Compatible with JDK 1.0+.
91 private static final long serialVersionUID = -2767605614048989439L;
93 /**
94 * The internal array used to hold members of a Vector. The elements are
95 * in positions 0 through elementCount - 1, and all remaining slots are null.
96 * @serial the elements
98 protected Object[] elementData;
101 * The number of elements currently in the vector, also returned by
102 * {@link #size}.
103 * @serial the size
105 protected int elementCount;
108 * The amount the Vector's internal array should be increased in size when
109 * a new element is added that exceeds the current size of the array,
110 * or when {@link #ensureCapacity} is called. If &lt;= 0, the vector just
111 * doubles in size.
112 * @serial the amount to grow the vector by
114 protected int capacityIncrement;
117 * Constructs an empty vector with an initial size of 10, and
118 * a capacity increment of 0
120 public Vector()
122 this(10, 0);
126 * Constructs a vector containing the contents of Collection, in the
127 * order given by the collection.
129 * @param c collection of elements to add to the new vector
130 * @throws NullPointerException if c is null
131 * @since 1.2
133 public Vector(Collection<? extends T> c)
135 elementCount = c.size();
136 elementData = c.toArray(new Object[elementCount]);
140 * Constructs a Vector with the initial capacity and capacity
141 * increment specified.
143 * @param initialCapacity the initial size of the Vector's internal array
144 * @param capacityIncrement the amount the internal array should be
145 * increased by when necessary, 0 to double the size
146 * @throws IllegalArgumentException if initialCapacity &lt; 0
148 public Vector(int initialCapacity, int capacityIncrement)
150 if (initialCapacity < 0)
151 throw new IllegalArgumentException();
152 elementData = new Object[initialCapacity];
153 this.capacityIncrement = capacityIncrement;
157 * Constructs a Vector with the initial capacity specified, and a capacity
158 * increment of 0 (double in size).
160 * @param initialCapacity the initial size of the Vector's internal array
161 * @throws IllegalArgumentException if initialCapacity &lt; 0
163 public Vector(int initialCapacity)
165 this(initialCapacity, 0);
169 * Copies the contents of the Vector into the provided array. If the
170 * array is too small to fit all the elements in the Vector, an
171 * {@link IndexOutOfBoundsException} is thrown without modifying the array.
172 * Old elements in the array are overwritten by the new elements.
174 * @param a target array for the copy
175 * @throws IndexOutOfBoundsException the array is not large enough
176 * @throws NullPointerException the array is null
177 * @see #toArray(Object[])
179 public synchronized void copyInto(Object[] a)
181 System.arraycopy(elementData, 0, a, 0, elementCount);
185 * Trims the Vector down to size. If the internal data array is larger
186 * than the number of Objects its holding, a new array is constructed
187 * that precisely holds the elements. Otherwise this does nothing.
189 public synchronized void trimToSize()
191 // Don't bother checking for the case where size() == the capacity of the
192 // vector since that is a much less likely case; it's more efficient to
193 // not do the check and lose a bit of performance in that infrequent case
195 T[] newArray = (T[]) new Object[elementCount];
196 System.arraycopy(elementData, 0, newArray, 0, elementCount);
197 elementData = newArray;
201 * Ensures that <code>minCapacity</code> elements can fit within this Vector.
202 * If <code>elementData</code> is too small, it is expanded as follows:
203 * If the <code>elementCount + capacityIncrement</code> is adequate, that
204 * is the new size. If <code>capacityIncrement</code> is non-zero, the
205 * candidate size is double the current. If that is not enough, the new
206 * size is <code>minCapacity</code>.
208 * @param minCapacity the desired minimum capacity, negative values ignored
210 public synchronized void ensureCapacity(int minCapacity)
212 if (elementData.length >= minCapacity)
213 return;
215 int newCapacity;
216 if (capacityIncrement <= 0)
217 newCapacity = elementData.length * 2;
218 else
219 newCapacity = elementData.length + capacityIncrement;
221 T[] newArray = (T[]) new Object[Math.max(newCapacity, minCapacity)];
223 System.arraycopy(elementData, 0, newArray, 0, elementCount);
224 elementData = newArray;
228 * Explicitly sets the size of the vector (but not necessarily the size of
229 * the internal data array). If the new size is smaller than the old one,
230 * old values that don't fit are lost. If the new size is larger than the
231 * old one, the vector is padded with null entries.
233 * @param newSize The new size of the internal array
234 * @throws ArrayIndexOutOfBoundsException if the new size is negative
236 public synchronized void setSize(int newSize)
238 // Don't bother checking for the case where size() == the capacity of the
239 // vector since that is a much less likely case; it's more efficient to
240 // not do the check and lose a bit of performance in that infrequent case
241 modCount++;
242 ensureCapacity(newSize);
243 if (newSize < elementCount)
244 Arrays.fill(elementData, newSize, elementCount, null);
245 elementCount = newSize;
249 * Returns the size of the internal data array (not the amount of elements
250 * contained in the Vector).
252 * @return capacity of the internal data array
254 public synchronized int capacity()
256 return elementData.length;
260 * Returns the number of elements stored in this Vector.
262 * @return the number of elements in this Vector
264 public synchronized int size()
266 return elementCount;
270 * Returns true if this Vector is empty, false otherwise
272 * @return true if the Vector is empty, false otherwise
274 public synchronized boolean isEmpty()
276 return elementCount == 0;
280 * Returns an Enumeration of the elements of this Vector. The enumeration
281 * visits the elements in increasing index order, but is NOT thread-safe.
283 * @return an Enumeration
284 * @see #iterator()
286 // No need to synchronize as the Enumeration is not thread-safe!
287 public Enumeration<T> elements()
289 return new Enumeration<T>()
291 private int i = 0;
293 public boolean hasMoreElements()
295 return i < elementCount;
298 @SuppressWarnings("unchecked")
299 public T nextElement()
301 if (i >= elementCount)
302 throw new NoSuchElementException();
303 return (T) elementData[i++];
309 * Returns true when <code>elem</code> is contained in this Vector.
311 * @param elem the element to check
312 * @return true if the object is contained in this Vector, false otherwise
314 public boolean contains(Object elem)
316 return indexOf(elem, 0) >= 0;
320 * Returns the first occurrence of <code>elem</code> in the Vector, or -1 if
321 * <code>elem</code> is not found.
323 * @param elem the object to search for
324 * @return the index of the first occurrence, or -1 if not found
326 public int indexOf(Object elem)
328 return indexOf(elem, 0);
332 * Searches the vector starting at <code>index</code> for object
333 * <code>elem</code> and returns the index of the first occurrence of this
334 * Object. If the object is not found, or index is larger than the size
335 * of the vector, -1 is returned.
337 * @param e the Object to search for
338 * @param index start searching at this index
339 * @return the index of the next occurrence, or -1 if it is not found
340 * @throws IndexOutOfBoundsException if index &lt; 0
342 public synchronized int indexOf(Object e, int index)
344 for (int i = index; i < elementCount; i++)
345 if (equals(e, elementData[i]))
346 return i;
347 return -1;
351 * Returns the last index of <code>elem</code> within this Vector, or -1
352 * if the object is not within the Vector.
354 * @param elem the object to search for
355 * @return the last index of the object, or -1 if not found
357 public int lastIndexOf(Object elem)
359 return lastIndexOf(elem, elementCount - 1);
363 * Returns the index of the first occurrence of <code>elem</code>, when
364 * searching backwards from <code>index</code>. If the object does not
365 * occur in this Vector, or index is less than 0, -1 is returned.
367 * @param e the object to search for
368 * @param index the index to start searching in reverse from
369 * @return the index of the Object if found, -1 otherwise
370 * @throws IndexOutOfBoundsException if index &gt;= size()
372 public synchronized int lastIndexOf(Object e, int index)
374 checkBoundExclusive(index);
375 for (int i = index; i >= 0; i--)
376 if (equals(e, elementData[i]))
377 return i;
378 return -1;
382 * Returns the Object stored at <code>index</code>.
384 * @param index the index of the Object to retrieve
385 * @return the object at <code>index</code>
386 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
387 * @see #get(int)
389 @SuppressWarnings("unchecked")
390 public synchronized T elementAt(int index)
392 checkBoundExclusive(index);
393 return (T) elementData[index];
397 * Returns the first element (index 0) in the Vector.
399 * @return the first Object in the Vector
400 * @throws NoSuchElementException the Vector is empty
402 @SuppressWarnings("unchecked")
403 public synchronized T firstElement()
405 if (elementCount == 0)
406 throw new NoSuchElementException();
408 return (T) elementData[0];
412 * Returns the last element in the Vector.
414 * @return the last Object in the Vector
415 * @throws NoSuchElementException the Vector is empty
417 @SuppressWarnings("unchecked")
418 public synchronized T lastElement()
420 if (elementCount == 0)
421 throw new NoSuchElementException();
423 return (T) elementData[elementCount - 1];
427 * Changes the element at <code>index</code> to be <code>obj</code>
429 * @param obj the object to store
430 * @param index the position in the Vector to store the object
431 * @throws ArrayIndexOutOfBoundsException the index is out of range
432 * @see #set(int, Object)
434 public void setElementAt(T obj, int index)
436 set(index, obj);
440 * Removes the element at <code>index</code>, and shifts all elements at
441 * positions greater than index to their index - 1.
443 * @param index the index of the element to remove
444 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size();
445 * @see #remove(int)
447 public void removeElementAt(int index)
449 remove(index);
453 * Inserts a new element into the Vector at <code>index</code>. Any elements
454 * at or greater than index are shifted up one position.
456 * @param obj the object to insert
457 * @param index the index at which the object is inserted
458 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
459 * @see #add(int, Object)
461 public synchronized void insertElementAt(T obj, int index)
463 checkBoundInclusive(index);
464 if (elementCount == elementData.length)
465 ensureCapacity(elementCount + 1);
466 modCount++;
467 System.arraycopy(elementData, index, elementData, index + 1,
468 elementCount - index);
469 elementCount++;
470 elementData[index] = obj;
474 * Adds an element to the Vector at the end of the Vector. The vector
475 * is increased by ensureCapacity(size() + 1) if needed.
477 * @param obj the object to add to the Vector
479 public synchronized void addElement(T obj)
481 if (elementCount == elementData.length)
482 ensureCapacity(elementCount + 1);
483 modCount++;
484 elementData[elementCount++] = obj;
488 * Removes the first (the lowest index) occurrence of the given object from
489 * the Vector. If such a remove was performed (the object was found), true
490 * is returned. If there was no such object, false is returned.
492 * @param obj the object to remove from the Vector
493 * @return true if the Object was in the Vector, false otherwise
494 * @see #remove(Object)
496 public synchronized boolean removeElement(Object obj)
498 int idx = indexOf(obj, 0);
499 if (idx >= 0)
501 remove(idx);
502 return true;
504 return false;
508 * Removes all elements from the Vector. Note that this does not
509 * resize the internal data array.
511 * @see #clear()
513 public synchronized void removeAllElements()
515 if (elementCount == 0)
516 return;
518 modCount++;
519 Arrays.fill(elementData, 0, elementCount, null);
520 elementCount = 0;
524 * Creates a new Vector with the same contents as this one. The clone is
525 * shallow; elements are not cloned.
527 * @return the clone of this vector
529 public synchronized Object clone()
533 Vector clone = (Vector) super.clone();
534 clone.elementData = (Object[]) elementData.clone();
535 return clone;
537 catch (CloneNotSupportedException ex)
539 // Impossible to get here.
540 throw new InternalError(ex.toString());
545 * Returns an Object array with the contents of this Vector, in the order
546 * they are stored within this Vector. Note that the Object array returned
547 * is not the internal data array, and that it holds only the elements
548 * within the Vector. This is similar to creating a new Object[] with the
549 * size of this Vector, then calling Vector.copyInto(yourArray).
551 * @return an Object[] containing the contents of this Vector in order
552 * @since 1.2
554 public synchronized Object[] toArray()
556 Object[] newArray = new Object[elementCount];
557 copyInto(newArray);
558 return newArray;
562 * Returns an array containing the contents of this Vector.
563 * If the provided array is large enough, the contents are copied
564 * into that array, and a null is placed in the position size().
565 * In this manner, you can obtain the size of a Vector by the position
566 * of the null element, if you know the vector does not itself contain
567 * null entries. If the array is not large enough, reflection is used
568 * to create a bigger one of the same runtime type.
570 * @param a an array to copy the Vector into if large enough
571 * @return an array with the contents of this Vector in order
572 * @throws ArrayStoreException the runtime type of the provided array
573 * cannot hold the elements of the Vector
574 * @throws NullPointerException if <code>a</code> is null
575 * @since 1.2
577 public synchronized <S> S[] toArray(S[] a)
579 if (a.length < elementCount)
580 a = (S[]) Array.newInstance(a.getClass().getComponentType(),
581 elementCount);
582 else if (a.length > elementCount)
583 a[elementCount] = null;
584 System.arraycopy(elementData, 0, a, 0, elementCount);
585 return a;
589 * Returns the element at position <code>index</code>.
591 * @param index the position from which an element will be retrieved
592 * @return the element at that position
593 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
594 * @since 1.2
596 public T get(int index)
598 return elementAt(index);
602 * Puts <code>element</code> into the Vector at position <code>index</code>
603 * and returns the Object that previously occupied that position.
605 * @param index the index within the Vector to place the Object
606 * @param element the Object to store in the Vector
607 * @return the previous object at the specified index
608 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
609 * @since 1.2
611 @SuppressWarnings("unchecked")
612 public synchronized T set(int index, T element)
614 checkBoundExclusive(index);
615 T temp = (T) elementData[index];
616 elementData[index] = element;
617 return temp;
621 * Adds an object to the Vector.
623 * @param o the element to add to the Vector
624 * @return true, as specified by List
625 * @since 1.2
627 public boolean add(T o)
629 addElement(o);
630 return true;
634 * Removes the given Object from the Vector. If it exists, true
635 * is returned, if not, false is returned.
637 * @param o the object to remove from the Vector
638 * @return true if the Object existed in the Vector, false otherwise
639 * @since 1.2
641 public boolean remove(Object o)
643 return removeElement(o);
647 * Adds an object at the specified index. Elements at or above
648 * index are shifted up one position.
650 * @param index the index at which to add the element
651 * @param element the element to add to the Vector
652 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
653 * @since 1.2
655 public void add(int index, T element)
657 insertElementAt(element, index);
661 * Removes the element at the specified index, and returns it.
663 * @param index the position from which to remove the element
664 * @return the object removed
665 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt;= size()
666 * @since 1.2
668 @SuppressWarnings("unchecked")
669 public synchronized T remove(int index)
671 checkBoundExclusive(index);
672 T temp = (T) elementData[index];
673 modCount++;
674 elementCount--;
675 if (index < elementCount)
676 System.arraycopy(elementData, index + 1, elementData, index,
677 elementCount - index);
678 elementData[elementCount] = null;
679 return temp;
683 * Clears all elements in the Vector and sets its size to 0.
685 public void clear()
687 removeAllElements();
691 * Returns true if this Vector contains all the elements in c.
693 * @param c the collection to compare to
694 * @return true if this vector contains all elements of c
695 * @throws NullPointerException if c is null
696 * @since 1.2
698 public synchronized boolean containsAll(Collection<?> c)
700 // Here just for the sychronization.
701 return super.containsAll(c);
705 * Appends all elements of the given collection to the end of this Vector.
706 * Behavior is undefined if the collection is modified during this operation
707 * (for example, if this == c).
709 * @param c the collection to append
710 * @return true if this vector changed, in other words c was not empty
711 * @throws NullPointerException if c is null
712 * @since 1.2
714 public synchronized boolean addAll(Collection<? extends T> c)
716 return addAll(elementCount, c);
720 * Remove from this vector all elements contained in the given collection.
722 * @param c the collection to filter out
723 * @return true if this vector changed
724 * @throws NullPointerException if c is null
725 * @since 1.2
727 public synchronized boolean removeAll(Collection<?> c)
729 // The NullPointerException is thrown implicitly when the Vector
730 // is not empty and c is null. The RI allows null arguments when
731 // the vector is empty. See Mauve test:
732 // gnu/testlet/java/util/Vector/removeAll.java
734 int i;
735 int j;
736 for (i = 0; i < elementCount; i++)
737 if (c.contains(elementData[i]))
738 break;
739 if (i == elementCount)
740 return false;
742 modCount++;
743 for (j = i++; i < elementCount; i++)
744 if (! c.contains(elementData[i]))
745 elementData[j++] = elementData[i];
746 elementCount -= i - j;
747 return true;
751 * Retain in this vector only the elements contained in the given collection.
753 * @param c the collection to filter by
754 * @return true if this vector changed
755 * @throws NullPointerException if c is null
756 * @since 1.2
758 public synchronized boolean retainAll(Collection<?> c)
760 // The NullPointerException is thrown implicitly when the Vector
761 // is not empty and c is null. The RI allows null arguments when
762 // the vector is empty. See Mauve test:
763 // gnu/testlet/java/util/Vector/retainAll.java
765 int i;
766 int j;
767 for (i = 0; i < elementCount; i++)
768 if (! c.contains(elementData[i]))
769 break;
770 if (i == elementCount)
771 return false;
773 modCount++;
774 for (j = i++; i < elementCount; i++)
775 if (c.contains(elementData[i]))
776 elementData[j++] = elementData[i];
777 elementCount -= i - j;
778 return true;
782 * Inserts all elements of the given collection at the given index of
783 * this Vector. Behavior is undefined if the collection is modified during
784 * this operation (for example, if this == c).
786 * @param c the collection to append
787 * @return true if this vector changed, in other words c was not empty
788 * @throws NullPointerException if c is null
789 * @throws ArrayIndexOutOfBoundsException index &lt; 0 || index &gt; size()
790 * @since 1.2
792 public synchronized boolean addAll(int index, Collection<? extends T> c)
794 checkBoundInclusive(index);
795 Iterator<? extends T> itr = c.iterator();
796 int csize = c.size();
798 modCount++;
799 ensureCapacity(elementCount + csize);
800 int end = index + csize;
801 if (elementCount > 0 && index != elementCount)
802 System.arraycopy(elementData, index,
803 elementData, end, elementCount - index);
804 elementCount += csize;
805 for ( ; index < end; index++)
806 elementData[index] = itr.next();
807 return (csize > 0);
811 * Compares this to the given object.
813 * @param o the object to compare to
814 * @return true if the two are equal
815 * @since 1.2
817 public synchronized boolean equals(Object o)
819 // Here just for the sychronization.
820 return super.equals(o);
824 * Computes the hashcode of this object.
826 * @return the hashcode
827 * @since 1.2
829 public synchronized int hashCode()
831 // Here just for the sychronization.
832 return super.hashCode();
836 * Returns a string representation of this Vector in the form
837 * "[element0, element1, ... elementN]".
839 * @return the String representation of this Vector
841 public synchronized String toString()
843 // Here just for the sychronization.
844 return super.toString();
848 * Obtain a List view of a subsection of this list, from fromIndex
849 * (inclusive) to toIndex (exclusive). If the two indices are equal, the
850 * sublist is empty. The returned list is modifiable, and changes in one
851 * reflect in the other. If this list is structurally modified in
852 * any way other than through the returned list, the result of any subsequent
853 * operations on the returned list is undefined.
854 * <p>
856 * @param fromIndex the index that the returned list should start from
857 * (inclusive)
858 * @param toIndex the index that the returned list should go to (exclusive)
859 * @return a List backed by a subsection of this vector
860 * @throws IndexOutOfBoundsException if fromIndex &lt; 0
861 * || toIndex &gt; size()
862 * @throws IllegalArgumentException if fromIndex &gt; toIndex
863 * @see ConcurrentModificationException
864 * @since 1.2
866 public synchronized List<T> subList(int fromIndex, int toIndex)
868 List<T> sub = super.subList(fromIndex, toIndex);
869 // We must specify the correct object to synchronize upon, hence the
870 // use of a non-public API
871 return new Collections.SynchronizedList<T>(this, sub);
875 * Removes a range of elements from this list.
876 * Does nothing when toIndex is equal to fromIndex.
878 * @param fromIndex the index to start deleting from (inclusive)
879 * @param toIndex the index to delete up to (exclusive)
880 * @throws IndexOutOfBoundsException if fromIndex &gt; toIndex
882 // This does not need to be synchronized, because it is only called through
883 // clear() of a sublist, and clear() had already synchronized.
884 protected void removeRange(int fromIndex, int toIndex)
886 int change = toIndex - fromIndex;
887 if (change > 0)
889 modCount++;
890 System.arraycopy(elementData, toIndex, elementData, fromIndex,
891 elementCount - toIndex);
892 int save = elementCount;
893 elementCount -= change;
894 Arrays.fill(elementData, elementCount, save, null);
896 else if (change < 0)
897 throw new IndexOutOfBoundsException();
901 * Checks that the index is in the range of possible elements (inclusive).
903 * @param index the index to check
904 * @throws ArrayIndexOutOfBoundsException if index &gt; size
906 private void checkBoundInclusive(int index)
908 // Implementation note: we do not check for negative ranges here, since
909 // use of a negative index will cause an ArrayIndexOutOfBoundsException
910 // with no effort on our part.
911 if (index > elementCount)
912 raiseBoundsError(index, " > ");
916 * Checks that the index is in the range of existing elements (exclusive).
918 * @param index the index to check
919 * @throws ArrayIndexOutOfBoundsException if index &gt;= size
921 private void checkBoundExclusive(int index)
923 // Implementation note: we do not check for negative ranges here, since
924 // use of a negative index will cause an ArrayIndexOutOfBoundsException
925 // with no effort on our part.
926 if (index >= elementCount)
927 raiseBoundsError(index, " >= ");
931 * Raise the ArrayIndexOfOutBoundsException.
933 * @param index the index of the access
934 * @param operator the operator to include in the error message
935 * @throws IndexOutOfBoundsException unconditionally
937 private void raiseBoundsError(int index, String operator)
939 // Implementaion note: put in a separate method to make the JITs job easier
940 // (separate common from uncommon code at method boundaries when trivial to
941 // do so).
942 throw new ArrayIndexOutOfBoundsException(index + operator + elementCount);
946 * Serializes this object to the given stream.
948 * @param s the stream to write to
949 * @throws IOException if the underlying stream fails
950 * @serialData just calls default write function
952 private synchronized void writeObject(ObjectOutputStream s)
953 throws IOException
955 s.defaultWriteObject();