📄 ivector.java
字号:
/**
* Returns the number of components in this vector.
*
* @return the number of components in this vector.
*/
public synchronized int size() {
return elementCount;
}
/**
* Tests if this vector has no components.
*
* @return <code>true</code> if and only if this vector has
* no components, that is, its size is zero;
* <code>false</code> otherwise.
*/
public synchronized boolean isEmpty() {
return elementCount == 0;
}
/**
* 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 int elementAt(int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " + elementCount);
}
return elementData[index];
}
/**
* 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.int)
*/
public synchronized void setElementAt(int obj, int index) {
if (index >= elementCount) {
throw new ArrayIndexOutOfBoundsException(index + " >= " +
elementCount);
}
elementData[index] = obj;
}
/**
* 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(int, 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, int)
* @see List
*/
public synchronized void insertElementAt(int 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(int) method
* (which is part of the List interface).
*
* @param obj the component to be added.
* @see #add(int)
* @see List
*/
public synchronized void addElement(int obj) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = obj;
}
public IVector getClonedIVector() {
IVector iVector = new IVector();
for (int i=0; i<this.size(); i++) {
iVector.addElement(this.elementAt(i));
}
return iVector;
}
/**
* Returns an array containing all of the elements in this Vector
* in the correct order.
*
* @since 1.2
*/
public synchronized int[] toArray() {
int[] result = new int[elementCount];
System.arraycopy(elementData, 0, result, 0, elementCount);
return result;
}
// 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 int 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 int set(int index, int element) {
if (index >= elementCount)
throw new ArrayIndexOutOfBoundsException(index);
int 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(int o) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = o;
return true;
}
/**
* 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, int element) {
insertElementAt(element, index);
}
public int getFirstIndex(int value) {
for (int i=0; i<this.size(); i++) {
if (this.elementAt(i)==value) return i;
}
return -1;
}
/**
* The number of times this list has been <i>structurally modified</i>.
* 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.<p>
*
* This field is used by the iterator and list iterator implementation
* returned by the <tt>iterator</tt> and <tt>listIterator</tt> methods.
* If the value of this field changes unexpectedly, the iterator (or list
* iterator) will throw a <tt>ConcurrentModificationException</tt> in
* response to the <tt>next</tt>, <tt>remove</tt>, <tt>previous</tt>,
* <tt>set</tt> or <tt>add</tt> operations. This provides
* <i>fail-fast</i> behavior, rather than non-deterministic behavior in
* the face of concurrent modification during iteration.<p>
*
* <b>Use of this field by subclasses is optional.</b> If a subclass
* wishes to provide fail-fast iterators (and list iterators), then it
* merely has to increment this field in its <tt>add(int, Object)</tt> and
* <tt>remove(int)</tt> methods (and any other methods that it overrides
* that result in structural modifications to the list). A single call to
* <tt>add(int, Object)</tt> or <tt>remove(int)</tt> must add no more than
* one to this field, or the iterators (and list iterators) will throw
* bogus <tt>ConcurrentModificationExceptions</tt>. If an implementation
* does not wish to provide fail-fast iterators, this field may be
* ignored.
*/
protected transient int modCount = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -