📄 vector.java
字号:
* the search at <code>index</code>, and testing for equality using * the <code>equals</code> method. * * @param elem an object. * @param index the non-negative index to start searching from. * @return the index of the first occurrence of the object argument in * this vector at position <code>index</code> or later in the * vector, that is, the smallest value <tt>k</tt> such that * <tt>elem.equals(elementData[k]) && (k >= index)</tt> is * <tt>true</tt>; returns <code>-1</code> if the object is not * found. (Returns <code>-1</code> if <tt>index</tt> >= the * current size of this <tt>Vector</tt>.) * @exception IndexOutOfBoundsException if <tt>index</tt> is negative. * @see Object#equals(Object) */ public synchronized int indexOf(Object elem, int index) { if (elem == null) { for (int i = index ; i < elementCount ; i++) if (elementData[i]==null) return i; } else { for (int i = index ; i < elementCount ; i++) if (elem.equals(elementData[i])) return i; } return -1; } /** * Returns the index of the last occurrence of the specified object in * this vector. * * @param elem the desired component. * @return the index of the last occurrence of the specified object in * this vector, that is, the largest value <tt>k</tt> such that * <tt>elem.equals(elementData[k])</tt> is <tt>true</tt>; * returns <code>-1</code> if the object is not found. */ public synchronized int lastIndexOf(Object elem) { return lastIndexOf(elem, elementCount-1); } /** * Searches backwards for the specified object, starting from the * specified index, and returns an index to it. * * @param elem the desired component. * @param index the index to start searching from. * @return the index of the last occurrence of the specified object in this * vector at position less than or equal to <code>index</code> in * the vector, that is, the largest value <tt>k</tt> such that * <tt>elem.equals(elementData[k]) && (k <= index)</tt> is * <tt>true</tt>; <code>-1</code> if the object is not found. * (Returns <code>-1</code> if <tt>index</tt> is negative.) * @exception IndexOutOfBoundsException if <tt>index</tt> is greater * than or equal to the current size of this vector. */ public synchronized int lastIndexOf(Object elem, int index) { if (index >= elementCount) throw new IndexOutOfBoundsException(index + " >= "+ elementCount); if (elem == null) { for (int i = index; i >= 0; i--) if (elementData[i]==null) return i; } else { for (int i = index; i >= 0; i--) if (elem.equals(elementData[i])) return i; } return -1; } /** * Returns the component at the specified index.<p> * * This method is identical in functionality to the get method * (which is part of the List interface). * * @param index an index into this vector. * @return the component at the specified index. * @exception ArrayIndexOutOfBoundsException if the <tt>index</tt> * is negative or not less than the current size of this * <tt>Vector</tt> object. * given. * @see #get(int) * @see List */ public synchronized Object elementAt(int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } return elementData[index]; } /** * Returns the first component (the item at index <tt>0</tt>) of * this vector. * * @return the first component of this vector. * @exception NoSuchElementException if this vector has no components. */ public synchronized Object firstElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData[0]; } /** * Returns the last component of the vector. * * @return the last component of the vector, i.e., the component at index * <code>size() - 1</code>. * @exception NoSuchElementException if this vector is empty. */ public synchronized Object lastElement() { if (elementCount == 0) { throw new NoSuchElementException(); } return elementData[elementCount - 1]; } /** * Sets the component at the specified <code>index</code> of this * vector to be the specified object. The previous component at that * position is discarded.<p> * * The index must be a value greater than or equal to <code>0</code> * and less than the current size of the vector. <p> * * This method is identical in functionality to the set method * (which is part of the List interface). Note that the set method reverses * the order of the parameters, to more closely match array usage. Note * also that the set method returns the old value that was stored at the * specified position. * * @param obj what the component is to be set to. * @param index the specified index. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see List * @see #set(int, java.lang.Object) */ public synchronized void setElementAt(Object obj, int index) { if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } elementData[index] = obj; } /** * Deletes the component at the specified index. Each component in * this vector with an index greater or equal to the specified * <code>index</code> is shifted downward to have an index one * smaller than the value it had previously. The size of this vector * is decreased by <tt>1</tt>.<p> * * The index must be a value greater than or equal to <code>0</code> * and less than the current size of the vector. <p> * * This method is identical in functionality to the remove method * (which is part of the List interface). Note that the remove method * returns the old value that was stored at the specified position. * * @param index the index of the object to remove. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see #remove(int) * @see List */ public synchronized void removeElementAt(int index) { modCount++; if (index >= elementCount) { throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount); } else if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } int j = elementCount - index - 1; if (j > 0) { System.arraycopy(elementData, index + 1, elementData, index, j); } elementCount--; elementData[elementCount] = null; /* to let gc do its work */ } /** * Inserts the specified object as a component in this vector at the * specified <code>index</code>. Each component in this vector with * an index greater or equal to the specified <code>index</code> is * shifted upward to have an index one greater than the value it had * previously. <p> * * The index must be a value greater than or equal to <code>0</code> * and less than or equal to the current size of the vector. (If the * index is equal to the current size of the vector, the new element * is appended to the Vector.)<p> * * This method is identical in functionality to the add(Object, int) method * (which is part of the List interface). Note that the add method reverses * the order of the parameters, to more closely match array usage. * * @param obj the component to insert. * @param index where to insert the new component. * @exception ArrayIndexOutOfBoundsException if the index was invalid. * @see #size() * @see #add(int, Object) * @see List */ public synchronized void insertElementAt(Object obj, int index) { modCount++; if (index > elementCount) { throw new ArrayIndexOutOfBoundsException(index + " > " + elementCount); } ensureCapacityHelper(elementCount + 1); System.arraycopy(elementData, index, elementData, index + 1, elementCount - index); elementData[index] = obj; elementCount++; } /** * Adds the specified component to the end of this vector, * increasing its size by one. The capacity of this vector is * increased if its size becomes greater than its capacity. <p> * * This method is identical in functionality to the add(Object) method * (which is part of the List interface). * * @param obj the component to be added. * @see #add(Object) * @see List */ public synchronized void addElement(Object obj) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = obj; } /** * Removes the first (lowest-indexed) occurrence of the argument * from this vector. If the object is found in this vector, each * component in the vector with an index greater or equal to the * object's index is shifted downward to have an index one smaller * than the value it had previously.<p> * * This method is identical in functionality to the remove(Object) * method (which is part of the List interface). * * @param obj the component to be removed. * @return <code>true</code> if the argument was a component of this * vector; <code>false</code> otherwise. * @see List#remove(Object) * @see List */ public synchronized boolean removeElement(Object obj) { modCount++; int i = indexOf(obj); if (i >= 0) { removeElementAt(i); return true; } return false; } /** * Removes all components from this vector and sets its size to zero.<p> * * This method is identical in functionality to the clear method * (which is part of the List interface). * * @see #clear * @see List */ public synchronized void removeAllElements() { modCount++; // Let gc do its work for (int i = 0; i < elementCount; i++) elementData[i] = null; elementCount = 0; } /** * Returns a clone of this vector. The copy will contain a * reference to a clone of the internal data array, not a reference * to the original internal data array of this <tt>Vector</tt> object. * * @return a clone of this vector. */ public synchronized Object clone() { try { Vector v = (Vector)super.clone(); v.elementData = new Object[elementCount]; System.arraycopy(elementData, 0, v.elementData, 0, elementCount); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } /** * Returns an array containing all of the elements in this Vector * in the correct order. * * @since 1.2 */ public synchronized Object[] toArray() { Object[] result = new Object[elementCount]; System.arraycopy(elementData, 0, result, 0, elementCount); return result; } /** * Returns an array containing all of the elements in this Vector in the * correct order; the runtime type of the returned array is that of the * specified array. If the Vector fits in the specified array, it is * returned therein. Otherwise, a new array is allocated with the runtime * type of the specified array and the size of this Vector.<p> * * If the Vector fits in the specified array with room to spare * (i.e., the array has more elements than the Vector), * the element in the array immediately following the end of the * Vector is set to null. This is useful in determining the length * of the Vector <em>only</em> if the caller knows that the Vector * does not contain any null elements. * * @param a the array into which the elements of the Vector are to * be stored, if it is big enough; otherwise, a new array of the * same runtime type is allocated for this purpose. * @return an array containing the elements of the Vector. * @exception ArrayStoreException the runtime type of a is not a supertype * of the runtime type of every element in this Vector. * @throws NullPointerException if the given array is null.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -