/**
* Int vector
* @author Bing
*
*/
public class IntVector implements Cloneable{
protected int increaseSize;
protected int data[];
protected int size = 0;
protected int capacity;
public IntVector() {
this(16,16);
}
public IntVector(int capacity, int increaseSize) {
this.increaseSize = increaseSize;
this.capacity = capacity;
this.data = new int[capacity];
}
public final int size() {
return size;
}
public final void setSize(int sz) {
size = sz;
}
public final void addElement(int value) {
if (size >= capacity) {
capacity += increaseSize;
int buf[] = new int[capacity];
System.arraycopy(data, 0, buf, 0, size);
data = buf;
}
data[size++] = value;
}
/**
*
* @param value The value
* @param p The index must be a value greater than or equal to 0 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.)
*/
public final void insertElementAt(int value, int p) {
if (size >= capacity) {
capacity += increaseSize;
int buf[] = new int[capacity];
System.arraycopy(data, 0, buf, 0, size );
data = buf;
}
if (p < size) {
System.arraycopy(data, p, data, p + 1, size - p);
data[p] = value;
}else{
data[size] = value;
}
size++;
}
public final void removeAllElements() {
size = 0;
}
public final void removeElementAt(int i) {
if(i>=size)
return;
System.arraycopy(data, i + 1, data, i, size-i-1);
size--;
}
public final void setElementAt(int value, int index) {
data[index] = value;
}
public final int elementAt(int i) {
return data[i];
}
public final boolean contains(int s) {
for (int i = 0; i < size; i++) {
if (data[i] == s)
return true;
}
return false;
}
public final int indexOf(int elem, int index) {
for (int i = index; i < size; i++) {
if (data[i] == elem)
return i;
}
return -1;
}
public final int indexOf(int elem) {
for (int i = 0; i < size; i++) {
if (data[i] == elem)
return i;
}
return -1;
}
public final int lastIndexOf(int elem) {
for (int i = (size - 1); i >= 0; i--) {
if (data[i] == elem)
return i;
}
return -1;
}
public Object clone() throws CloneNotSupportedException {
IntVector nv = new IntVector();
nv.data = new int[this.capacity];
nv.capacity = this.capacity;
nv.size = this.size;
nv.increaseSize = this.increaseSize;
System.arraycopy(this.data, 0, nv.data, 0, this.size);
return nv;
}
}
IntVector
最新推荐文章于 2023-10-18 16:49:11 发布