vector.java
来自「《移动Agent技术》一书的所有章节源代码。」· Java 代码 · 共 566 行 · 第 1/2 页
JAVA
566 行
/**
* 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; returns <code>-1</code> if the object is not found.
* @since JDK1.0
*/
public final 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 <code>index</code> in the vector;
* <code>-1</code> if the object is not found.
* @since JDK1.0
*/
public final synchronized int lastIndexOf(Object elem, int index) {
for (int i = index ; i >= 0 ; i--) {
if (elem.equals(elementData[i])) {
return i;
}
}
return -1;
}
/**
* Returns the component at the specified index.
*
* @param index an index into this vector.
* @return the component at the specified index.
* @exception ArrayIndexOutOfBoundsException if an invalid index was
* given.
* @since JDK1.0
*/
public final synchronized Object elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
/* Since try/catch is free, except when the exception is thrown,
put in this extra try/catch to catch negative indexes and
display a more informative error message. This might not
be appropriate, especially if we have a decent debugging
environment - JP. */
try {
return elementData[index];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException(index + " < 0");
}
}
/**
* Returns the first component of this vector.
*
* @return the first component of this vector.
* @exception NoSuchElementException if this vector has no components.
* @since JDK1.0
*/
public final 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.
* @since JDK1.0
*/
public final 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.
*
* @param obj what the component is to be set to.
* @param index the specified index.
* @exception ArrayIndexOutOfBoundsException if the index was invalid.
* @see java.util.Vector#size()
* @since JDK1.0
*/
public final 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.
* <p>
* The index must be a value greater than or equal to <code>0</code>
* and less than the current size of the vector.
*
* @param index the index of the object to remove.
* @exception ArrayIndexOutOfBoundsException if the index was invalid.
* @see java.util.Vector#size()
* @since JDK1.0
*/
public final synchronized void removeElementAt(int index) {
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.
*
* @param obj the component to insert.
* @param index where to insert the new component.
* @exception ArrayIndexOutOfBoundsException if the index was invalid.
* @see java.util.Vector#size()
* @since JDK1.0
*/
public final synchronized void insertElementAt(Object obj, int index) {
int newcount = elementCount + 1;
if (index >= newcount) {
throw new ArrayIndexOutOfBoundsException(index
+ " > " + elementCount);
}
if (newcount > elementData.length) {
ensureCapacityHelper(newcount);
}
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.
*
* @param obj the component to be added.
* @since JDK1.0
*/
public final synchronized void addElement(Object obj) {
int newcount = elementCount + 1;
if (newcount > elementData.length) {
ensureCapacityHelper(newcount);
}
elementData[elementCount++] = obj;
}
/**
* Removes the first 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.
*
* @param obj the component to be removed.
* @return <code>true</code> if the argument was a component of this
* vector; <code>false</code> otherwise.
* @since JDK1.0
*/
public final synchronized boolean removeElement(Object obj) {
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.
*
* @since JDK1.0
*/
public final synchronized void removeAllElements() {
for (int i = 0; i < elementCount; i++) {
elementData[i] = null;
}
elementCount = 0;
}
/**
* Returns a clone of this vector.
*
* @return a clone of this vector.
* @since JDK1.0
*/
public synchronized Object clone() {
try {
Vector v = (Vector)super.clone();
v.elementData = new Object[elementCount];
System.arraycopy(elementData, 0, v.elementData, 0, elementCount);
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError();
}
}
/**
* Returns a string representation of this vector.
*
* @return a string representation of this vector.
* @since JDK1.0
*/
public final synchronized String toString() {
int max = size() - 1;
StringBuffer buf = new StringBuffer();
Enumeration e = elements();
buf.append("[");
for (int i = 0 ; i <= max ; i++) {
String s = e.nextElement().toString();
buf.append(s);
if (i < max) {
buf.append(", ");
}
}
buf.append("]");
return buf.toString();
}
}
final
class VectorEnumerator implements Enumeration {
Vector vector;
int count;
VectorEnumerator(Vector v) {
vector = v;
count = 0;
}
public boolean hasMoreElements() {
return count < vector.elementCount;
}
public Object nextElement() {
synchronized (vector) {
if (count < vector.elementCount) {
return vector.elementData[count++];
}
}
throw new NoSuchElementException("VectorEnumerator");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?