顺序表的实现与操作

顺序表的常用操作
定义及创建顺序表
public class SqList<T> {
    private T[] element;
    private int length;
    
//    public int getCapacity(){
//        return element.length;
//    }

    // 创建容量为length的空表
    public SqList(int length) {
        this.element = (T[]) new Object[length];
    }

    // 创建默认容量的空表
    public SqList() {
        this(64);
    }

    // 创建顺序表
    public SqList(T[] values) {
        this(values.length);
        for (int i = 0; i < values.length; i++) {
            this.element[i] = values[i];
        }
        this.length = element.length;
    }
}
判断是否为空
// 判断顺序表是否为空
public boolean isEmpty() {
    return this.length == 0;
}
// 插入x作为第i(1≤i)个元素,返回x序号
public int insert(int i, T x) {
    if (x == null) {
        throw new NullPointerException("x == null");
    }else if (i < 1) { // 插入位置i容错,插入在最前
        i = 1;
    }else if (i > this.length + 1) {    // 插入位置i容错,插入在最后
        i = this.length + 1;
    }
    T[] source = this.element;
    // 若数组空间已满,则扩容顺序表的数组
    if (this.length == element.length) {
        // 重新申请一个容量更大的数组
        this.element =(T[]) new Object[source.length * 2];
        // 复制当前数组的i-1个元素
        for (int j = 0; j < i - 1; j++) {
            this.element[j] = source[j];
        }
    }
    // 从i开始至表尾的元素向后移动
    for (int j = this.length - 1; j >= i - 1; j--) {
        this.element[j + 1] = source[j];
    }
    this.element[i - 1] = x;
    this.length++;
    return i;
}
// 顺序表表尾插入x元素,返回x序号
public int insert(T x) {
    return this.insert(this.length + 1, x);
}
// 删除第i(1≤i≤length)个元素,返回被删除的元素,若i越界,则返回null
public T remove(int i) {
    if (this.length > 0 && i > 0 && i <= this.length) {
        // old中存储被删除元素
        T old = this.element[i - 1];
        // 将第i个元素后面的元素前移
        for (int j = i; j < this.length; j++) {
            this.element[j - 1] = this.element[j];
        }
        // 将原最后一个元素设置为空
        this.element[this.length - 1] = null;
        this.length--;
        return old;
    }
    return null;
}
// 删除顺序表所有元素
public void clear() {
    // 设置长度为0,未释放数组空间
    this.length = 0;
}
// 设置第i(1≤i≤length)个元素为x
public void set(int i, T x) {
    if (x == null) {
        // 抛出空指针异常
        throw new NullPointerException("x == null");
    }else if (i > 0 && i <= this.length) {
        this.element[i-1] = x;
    }else {
        // 抛出序号越界异常
        throw new IndexOutOfBoundsException(i + "序号越界");
    }
}
// 返回第i(1≤i≤length)个元素,越界返回null
public T get(int i){
    if (i > 0 && i <= this.length) {
        return this.element[i-1];
    }else {
        return null;
    }
}
// 返回顺序表所有元素描述的字符串
public String toString() {
    String str = this.getClass().getName() + "(";
    if (this.length > 0) {
        str += this.element[0].toString();
    }
    for (int i = 1; i < this.length; i++) {
        str += "," + this.element[i].toString();
    }
    return str + ")";
}
// 顺序表查找首次出现的与key相等的元素,返回元素序号i(1≤i≤length),查找不成功则返回-1
public int search(T key) {
    if (key == null) {
        throw new NullPointerException("key == null");
    }
    for (int i = 0; i < this.length; i++) {
        if (key.equals(this.element[i])) {
            return i + 1;
        }
    }
    return -1;
}
// 判断是否包含关键字为key的元素
public boolean contains(T key) {
    return this.search(key) != -1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值