【Java数据结构】顺序表的代码实现

顺序表

  1. 顺序表其实就是一个写到类中的数组。写到类中就可以面向对象了。实现增删改查。
  2. 顺序表特点
    1. 物理上和逻辑上都是连续的
    2. 插入和删除元素,必须移动元素。(时间复杂度:O(N))
    3. 扩容也是问题
    4. 可以实现随机读取(查找时间复杂度:O(1))
  3. 顺序表的代码目录
    public class SeqList {
    // 打印顺序表
    public void display() { }
    // 在 pos 位置新增元素
    public void add(int pos, int data) { }
    // 判定是否包含某个元素
    public boolean contains(int toFind) { return true; }
    // 查找某个元素对应的位置
    public int search(int toFind) { return -1; }
    // 获取 pos 位置的元素
    public int getPos(int pos) { return -1; }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) { }
    //删除第一次出现的关键字key
    public void remove(int toRemove) { }
    // 获取顺序表长度
    public int size() { return 0; }
    // 清空顺序表
    public void clear() { }
    }
    
  4. 顺序表类的具体代码实现:
public class MyArrayList {
    public int[] elem;
    public int usedSize; //当前数组有效数组的个数

    public MyArrayList() {
        this.elem = new int[10];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    // 获取顺序表的有效长度
    public int size() {
        return this.usedSize;
    }

    //判断顺序表是否满了
    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //1.判断pos是否合法:pos<0 || pos > usedSize(前面没有元素)就不能放
        if (pos < 0 || pos > usedSize) {
            System.out.println("pos 位置不合法!");
            return;
        }
        //2.判断满不满,如果满了,需要扩容。(Java没有扩容失败的问题)
        if (this.isFull()) {
            this.elem = Arrays.copyOf(this.elem, 2*this.elem.length);
        }
        //3.移动元素,给pos位置腾位置
        for (int i = this.usedSize-1; i >= pos; i++) {
            this.elem[i+1] = this.elem[i];
        }
        /*
        //或者这样
        for (int i = this.usedSize; i > pos; i--) {
            this.elem[i] =  this.elem[i-1];
        }
         */
        //4.插入,usedSize++
        this.elem[pos] = data;
        usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置,找不到返回-1
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        //1.判断pos是否合法
        if (pos < 0 || pos >= this.usedSize) {
            System.out.println("pos 位置不合法!");
            return -1; //业务上的处理这里暂时不考虑,后期这里可以抛出异常
        }
        //2.判断顺序表是否为空
        if (isEmpty()) {
            return -1;
        }
        return this.elem[pos];
    }

    // 给 pos 位置的元素 设为/更新为 value
    public void setPos(int pos, int value) {
        if (pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法");
            return;
        }
        if (isEmpty()) {
            System.out.println("顺序表为空!");
            return;
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        //1. 判断顺序表是否为空
        if (isEmpty()) {
            System.out.println("顺序表为空");
            return;
        }
        //2. 查找要删除的数字toRemove的下标index
        int index = this.search(toRemove);
        if (index == -1) {
            System.out.println("没有要找的数字!");
            return;
        }
        //3. 从index的下一位开始,逐步往前覆盖。useSize-1;
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize -= 1;
    }

    // 清空顺序表
    public void clear() {
        //如果是简单类型的顺序表(只是存的数字、字符等)
        this.usedSize = 0;
        //但如果是引用类型的顺序表(存的是地址),必须将每个元素置为null
        /*
        for (int i = 0; i < usedSize; i++) {
            this.elem[i] = null;
        }
        this.usedSize = 0;
         */
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值