📄 vector.java
字号:
* @since 1.2 */ public synchronized Object[] toArray(Object a[]) { if (a.length < elementCount) a = (Object[])java.lang.reflect.Array.newInstance( a.getClass().getComponentType(), elementCount); System.arraycopy(elementData, 0, a, 0, elementCount); if (a.length > elementCount) a[elementCount] = null; return a; } // Positional Access Operations /** * Returns the element at the specified position in this Vector. * * @param index index of element to return. * @return object at the specified index * @exception ArrayIndexOutOfBoundsException index is out of range (index * < 0 || index >= size()). * @since 1.2 */ public synchronized Object get(int index) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); return elementData[index]; } /** * Replaces the element at the specified position in this Vector with the * specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @exception ArrayIndexOutOfBoundsException index out of range * (index < 0 || index >= size()). * @since 1.2 */ public synchronized Object set(int index, Object element) { if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object oldValue = elementData[index]; elementData[index] = element; return oldValue; } /** * Appends the specified element to the end of this Vector. * * @param o element to be appended to this Vector. * @return true (as per the general contract of Collection.add). * @since 1.2 */ public synchronized boolean add(Object o) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = o; return true; } /** * Removes the first occurrence of the specified element in this Vector * If the Vector does not contain the element, it is unchanged. More * formally, removes the element with the lowest index i such that * <code>(o==null ? get(i)==null : o.equals(get(i)))</code> (if such * an element exists). * * @param o element to be removed from this Vector, if present. * @return true if the Vector contained the specified element. * @since 1.2 */ public boolean remove(Object o) { return removeElement(o); } /** * Inserts the specified element at the specified position in this Vector. * Shifts the element currently at that position (if any) and any * subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted. * @param element element to be inserted. * @exception ArrayIndexOutOfBoundsException index is out of range * (index < 0 || index > size()). * @since 1.2 */ public void add(int index, Object element) { insertElementAt(element, index); } /** * Removes the element at the specified position in this Vector. * shifts any subsequent elements to the left (subtracts one from their * indices). Returns the element that was removed from the Vector. * * @exception ArrayIndexOutOfBoundsException index out of range (index * < 0 || index >= size()). * @param index the index of the element to removed. * @return element that was removed * @since 1.2 */ public synchronized Object remove(int index) { modCount++; if (index >= elementCount) throw new ArrayIndexOutOfBoundsException(index); Object oldValue = elementData[index]; int numMoved = elementCount - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--elementCount] = null; // Let gc do its work return oldValue; } /** * Removes all of the elements from this Vector. The Vector will * be empty after this call returns (unless it throws an exception). * * @since 1.2 */ public void clear() { removeAllElements(); } // Bulk Operations /** * Returns true if this Vector contains all of the elements in the * specified Collection. * * @param c a collection whose elements will be tested for containment * in this Vector * @return true if this Vector contains all of the elements in the * specified collection. * @throws NullPointerException if the specified collection is null. */ public synchronized boolean containsAll(Collection c) { return super.containsAll(c); } /** * Appends all of the elements in the specified Collection to the end of * this Vector, in the order that they are returned by the specified * Collection's Iterator. The behavior of this operation is undefined if * the specified Collection is modified while the operation is in progress. * (This implies that the behavior of this call is undefined if the * specified Collection is this Vector, and this Vector is nonempty.) * * @param c elements to be inserted into this Vector. * @return <tt>true</tt> if this Vector changed as a result of the call. * @throws NullPointerException if the specified collection is null. * @since 1.2 */ public synchronized boolean addAll(Collection c) { modCount++; Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); System.arraycopy(a, 0, elementData, elementCount, numNew); elementCount += numNew; return numNew != 0; } /** * Removes from this Vector all of its elements that are contained in the * specified Collection. * * @param c a collection of elements to be removed from the Vector * @return true if this Vector changed as a result of the call. * @throws NullPointerException if the specified collection is null. * @since 1.2 */ public synchronized boolean removeAll(Collection c) { return super.removeAll(c); } /** * Retains only the elements in this Vector that are contained in the * specified Collection. In other words, removes from this Vector all * of its elements that are not contained in the specified Collection. * * @param c a collection of elements to be retained in this Vector * (all other elements are removed) * @return true if this Vector changed as a result of the call. * @throws NullPointerException if the specified collection is null. * @since 1.2 */ public synchronized boolean retainAll(Collection c) { return super.retainAll(c); } /** * Inserts all of the elements in in the specified Collection into this * Vector at the specified position. Shifts the element currently at * that position (if any) and any subsequent elements to the right * (increases their indices). The new elements will appear in the Vector * in the order that they are returned by the specified Collection's * iterator. * * @param index index at which to insert first element * from the specified collection. * @param c elements to be inserted into this Vector. * @return <tt>true</tt> if this Vector changed as a result of the call. * @exception ArrayIndexOutOfBoundsException index out of range (index * < 0 || index > size()). * @throws NullPointerException if the specified collection is null. * @since 1.2 */ public synchronized boolean addAll(int index, Collection c) { modCount++; if (index < 0 || index > elementCount) throw new ArrayIndexOutOfBoundsException(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityHelper(elementCount + numNew); int numMoved = elementCount - index; if (numMoved > 0) System.arraycopy(elementData, index, elementData, index + numNew, numMoved); System.arraycopy(a, 0, elementData, index, numNew); elementCount += numNew; return numNew != 0; } /** * Compares the specified Object with this Vector for equality. Returns * true if and only if the specified Object is also a List, both Lists * have the same size, and all corresponding pairs of elements in the two * Lists are <em>equal</em>. (Two elements <code>e1</code> and * <code>e2</code> are <em>equal</em> if <code>(e1==null ? e2==null : * e1.equals(e2))</code>.) In other words, two Lists are defined to be * equal if they contain the same elements in the same order. * * @param o the Object to be compared for equality with this Vector. * @return true if the specified Object is equal to this Vector */ public synchronized boolean equals(Object o) { return super.equals(o); } /** * Returns the hash code value for this Vector. */ public synchronized int hashCode() { return super.hashCode(); } /** * Returns a string representation of this Vector, containing * the String representation of each element. */ public synchronized String toString() { return super.toString(); } /** * Returns a view of the portion of this List between fromIndex, * inclusive, and toIndex, exclusive. (If fromIndex and ToIndex are * equal, the returned List is empty.) The returned List is backed by this * List, so changes in the returned List are reflected in this List, and * vice-versa. The returned List supports all of the optional List * operations supported by this List.<p> * * This method eliminates the need for explicit range operations (of * the sort that commonly exist for arrays). Any operation that expects * a List can be used as a range operation by operating on a subList view * instead of a whole List. For example, the following idiom * removes a range of elements from a List: * <pre> * list.subList(from, to).clear(); * </pre> * Similar idioms may be constructed for indexOf and lastIndexOf, * and all of the algorithms in the Collections class can be applied to * a subList.<p> * * The semantics of the List returned by this method become undefined if * the backing list (i.e., this List) is <i>structurally modified</i> in * any way other than via the returned List. (Structural modifications are * those that change the size of the List, or otherwise perturb it in such * a fashion that iterations in progress may yield incorrect results.) * * @param fromIndex low endpoint (inclusive) of the subList. * @param toIndex high endpoint (exclusive) of the subList. * @return a view of the specified range within this List. * @throws IndexOutOfBoundsException endpoint index value out of range * <code>(fromIndex < 0 || toIndex > size)</code> * @throws IllegalArgumentException endpoint indices out of order * <code>(fromIndex > toIndex)</code> */ public synchronized List subList(int fromIndex, int toIndex) { return Collections.synchronizedList(super.subList(fromIndex, toIndex), this); } /** * Removes from this List all of the elements whose index is between * fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding * elements to the left (reduces their index). * This call shortens the ArrayList by (toIndex - fromIndex) elements. (If * toIndex==fromIndex, this operation has no effect.) * * @param fromIndex index of first element to be removed. * @param toIndex index after last element to be removed. */ protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = elementCount - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // Let gc do its work int newElementCount = elementCount - (toIndex-fromIndex); while (elementCount != newElementCount) elementData[--elementCount] = null; } /** * Save the state of the <tt>Vector</tt> instance to a stream (that * is, serialize it). This method is present merely for synchronization. * It just calls the default readObject method. */ private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { s.defaultWriteObject(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -