超详细!JAVA实现顺序表类

文章展示了如何在Java中实现一个名为Seqlist的类,该类支持增、删、改、查以及判断是否为空等基本操作。类使用泛型,具有不同构造函数以适应不同的初始化需求,并包含扩容机制以处理元素插入。Test类用于测试Seqlist的功能,例如删除元素和打印序列表。
摘要由CSDN通过智能技术生成

Seqlist类


/*
增、删、改、查,判断是否为空
 */

public class Seqlist<T> {

    protected int n;//顺序表元素个数
    protected Object[] element;//顺序表元素

    public static final int MIN_CAPCITY = 16;//顺序表最小容量


    //构造函数指定了长度
    public Seqlist(int lenth) {
        if (lenth < MIN_CAPCITY) {
            lenth = MIN_CAPCITY;
        }
        //System.out.println("已经调用");
        this.element = new Object[lenth];
        this.n = 0;

    }

    //构造函数什么都未指定,默认长度
    //如果未指定长度,this方法自动调用其他构造方法
    public Seqlist() {

        this(MIN_CAPCITY);

    }

    //构造函数指定数组
    public Seqlist(T[] values) {

        this(values.length * 2);//this方法调用指定长度的构造方法
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                this.element[this.n++] = values[i];
            }
        }
    }


    /*
     public boolean isEmpty()
     判断是否为空
     return:  是否为空
     */
    public boolean isEmpty() {
        return (this.n == 0);
    }


    /*
    public T get(int i)
    指定元素,返回元素
    i:要返回元素的索引
     */
    public T get(int i) {

        if (i >= 0 && i <= n) {
            return (T) this.element[i];
        }

        return null;
    }


    /*
    public void set(int i,T x)
    指定元素进行赋值
    i:指定元素位置索引
    x:元素的值
     */
    public void set(int i, T x) {

        if (x == null) {
            throw new NullPointerException("X=NULL");
        }

        if (i >= 0 && i <= this.n) {
            element[i] = x;
        } else {
            throw new ArrayIndexOutOfBoundsException(i + "");
        }

    }


    /*
     public int getSize()
     返回顺序表长度
     return:返回元素的长度,如果没有则返回-1
     */
    public int getSize() {
        if (this.n >= 0) {
            return this.n;
        }

        return -1;
        //return null;
    }

    /*
    public int insert(int k, T e)
    指定索引,插入元素
    k:指定索引
    e:要插入的元素
    return;返回插入的索引
     */
    public int insert(int k, T e) {
        if (e == null) {return -1;}// 为空则return-1
        if (k < 0) {k = 0;} //如果小于0,就查到头部
        if (k >this.n) {k = this.n;} //如果要插入的位置大于当前数组元素数量,就插到尾部

        //数组扩容
        //如果没有扩容,source就是创建了一个相同的数组,为下面的插入进行准备
        //如果做了扩容,也是一样
        Object[] source = this.element;//如果不扩容,操作source和element都一样
        if(this.n == this.element.length){
            this.element = new Object[source.length*2];//进行扩容
            for (int j = 0;  j < this.n; j++) {
                element[j] = source[j];//进行赋值
            }
        }

        //进行插入
        //利用source进行插入
        int m = 0;
        for (int j = this.n - 1; j > k; j--) {
            this.element[j+1] = source[j];
        }
        this.element[k] = e;
        this.n++;


        return k;
    }

    /*
    public T delete(int index)
    删除,并返回删除的元素
    index:要删除元素的索引
    return:被删除的元素
     */
    public T delete(int index){

        //没有元素,无发删除
        //大于存储的元素数量无法删除
        //负数索引不行
        if( n == 0 || index >n || index < 0){
            return null;
        }

        //存储要删除的元素,作为返回值
        T temp = (T)element[index];

        //如果是删除尾部元素,直接删除
        if( index == n ){
             this.element[index] = null;
        }

        //进行删除
        for (int k = index; k < this.n - 1; k++){
            //T temp;
            element[k] = element[k + 1];
        }
        element[this.n-1] = null;
        this.n--;
        return temp;
    }


    /*
    public void clear()
    清空所有元素
     */
    public void clear(){ this.n = 0;}

    /*
    public int search(T key)
    进行查找
    key;要查找的元素
    return: 如果找到该元素,则返回该元素索引
    否则,返回-1,为没有找到该元素
     */

    public int search(T key){

        for (int i = 0; i < this.n; i++) {

            if(key.equals(this.element[i])){
                return i;
            }
        }
        return -1;


    }
    @Override
    public String toString() {
        String str = this.getClass().getName() + "(";
        if (this.n > 0) {
            str += this.element[0].toString();
        }

        for (int i = 1; i < this.n; i++) {
            str += "," + this.element[i].toString();
        }
        return str + ")";
    }
}

Test 类进行测试

public class Test{
    public static void main(String[] args) {
        Integer [] str = {1,2,3,4};
        Seqlist seqlist = new Seqlist<>(str);

        System.out.println(seqlist.delete(1));//进行删除
        System.out.println(seqlist);
    }




}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值