顺序表

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成据的增删查改。
顺序表一般可分为:静态顺序表(使用定长数组存储);动态顺序表(使用动态开辟的数组存储)。
静态顺序表适用于确定知道需要存多少数据的场景.
静态顺序表的定长数组导致N定大了,空间开多了浪费,开少了不够用,相比之下动态顺序表更加灵活,根据需要动态分配内存大小。
动态顺序表的实现

public class MyArrayList {
    public int[] elem;
    public int usedSize;
    public MyArrayList() {
        this.elem = new int[10];
        this.usedSize = 0;
    }
    public MyArrayList(int capcity) {
        this.elem = new int[capcity];
        this.usedSize = 0;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if (pos < 0 || pos > this.usedSize){
            System.out.println(pos+"不合法!");
            return;
        }
        if(this.usedSize == this.elem.length){
            System.out.println("顺序表已满!");
            return;
        }
        for (int i = this.usedSize - 1;i >= pos;i--){
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    // 打印顺序表
    public void display() {
        for (int i = 0;i < this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0;i < this.usedSize;i++){
            if (this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0;i < this.usedSize;i++){
            if (this.elem[i] == toFind){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if (pos < 0 || pos >= this.usedSize){
            System.out.print(pos+"不合法");
            return -1;
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (pos < 0 || pos >= this.usedSize){
            System.out.print(pos+"不合法");
        }
        this.elem[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int index = search(toRemove);
        if (index == -1){
            System.out.println("该数字不存在!");
            return;
        }
        for (int i = index;i < this.usedSize - 1;i++){
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
    //删除所有的关键字Key
    /*public void removeAll(int key){
        for (int i = 0;i < this.usedSize+i;i++){
            remove(key);
            boolean flag =contains(key);
            if (flag == false)return;
        }
    }*/
    //删除所有关键字key
    public void removeAll(int key){
        for (int i = this.usedSize-1;i > 0;i--){
            remove(key);
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值