IntVector

/**
 * 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;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值