1. 首页
  2. 软件开发
  3. Java数据结构——代码实现顺序表的操作

Java数据结构——代码实现顺序表的操作

  • 发布于 2024-09-19
  • 0 次阅读

作者:敲代码の流川枫

博客主页:流川枫的博客

专栏:和我一起学java

语录:Stay hungry stay foolish

工欲善其事必先利其器,给大家介绍一款超牛的斩获大厂offer利器——牛客网

点击免费注册和我一起刷题吧

文章目录

顺序表

1.获取顺序表长度

2.打印顺序表中的所有元素

3.新增一个元素,默认在最后新增

4.在 pos 位置新增元素

5.判定是否包含某个元素

6.查找某个元素对应的位置

7.获取 pos 位置的元素

8.给 pos 位置的元素更新为 value

9.删除第一次出现的关键字key

10.清空顺序表



顺序表

顺序表是用一段物理地址连续 的,存储单元依次存储数据 的线性结构。是在数组上完成数据的增删查改

接下来我们自己实现一些接口对数组进行增删查改的操作

创建一个类:

public class MyArrayList {
    public int[] elem;//数组
    public int usedSize;//记录有效数据的个数
    public static final int DEFAULT_SIZE = 10;
    public MyArrayList(int[] elem){
        this.elem = new int[DEFAULT_SIZE];
    }

1.获取顺序表长度

public int size() {
        return this.usedSize;
}

2.打印顺序表中的所有元素

public void disPlay(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i]+" ");
        }
        System.out.println();
}

3.新增一个元素,默认在最后新增

步骤:检查当前顺序表是否满了;如果满了进行扩容;然后将元素放进去;usedSize++;

判满:

public boolean isFull(int[] elem){
        if(size()>=this.usedSize){
            return true;
        }
        else return false;
}

扩容:

 public void add(int data){
        if(isFull()){
            this.elem = 
                    Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //添加数据
        this.elem[this.usedSize] = data;
        //有效数据加一
        this.usedSize++;

}

测试:

public class TestList {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.disPlay();
    }
}

4.在 pos 位置新增元素

注意:负数下标不能当Pos;不能超过数组长度插入;不能间隔着插入,即插入的位置前面一定有元素;

还是先判断数组是否满,满了扩容;然后判断pos位置是否合法;不合法抛出异常;插入后将其他元素后移

 public void add(int pos, int data) {
        if(isFull()){
            System.out.println("满了");
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        if(pos<0||pos>this.usedSize){
            System.out.println("pos位置不合法");
            throw new PosworongfulException("pos位置不合法");
        }
        //pos一定合法
        // 挪动数据
        for (int i = this.usedSize-1; i>=pos  ; i--) {
            this.elem[i+1] = this.elem[i];
        }
        //插入数据
        this.elem[pos] = data;
        //usedSize++
        this.usedSize++;
}

添加10到1位置

try {
                myArrayList.add(1,10);
        }
        catch (PosworongfulException e){
                e.printStackTrace();
        }
                myArrayList.disPlay();

在10位置添加数据

try {
                myArrayList.add(10,10);
        }
        catch (PosworongfulException e){
                e.printStackTrace();
        }
                myArrayList.disPlay();

5. 判定是否包含某个元素

public boolean contains(int toFind) {
        for (int i = 0; i < this.size(); i++) {
            if(elem[i]==toFind){
                return true;
            }
        }
        return false;
}


    System.out.println("-------------");
    System.out.println(myArrayList.contains(10));
    System.out.println(myArrayList.contains(100));

6.查找某个元素对应的位置

public int indexOf(int toFind) {
        for (int i = 0; i < this.size(); i++) {
            if(this.elem[i]==toFind){
                return i;
            }
        }
        return -1;
}

        System.out.println("-------------");
        System.out.println(myArrayList.indexOf(3));

7.获取 pos 位置的元素

//判断为空
    public boolean isEmpty(){
        return size()==0;
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        if(isEmpty()){
            throw new EmptyException("当前顺序表为空");
        }
        if(pos<0||pos>=this.usedSize){
            throw new PosworongfulException("pos位置不合法");
        }
        return this.elem[pos];
    }

    System.out.println("------------");
    System.out.println(myArrayList.get(0));
     try {
          myArrayList.get(10);
     }catch (PosworongfulException e){
           e.printStackTrace();
     }

8.给 pos 位置的元素更新为 value

public void set(int pos, int value) {
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        if(pos<0||pos>=this.usedSize){
            throw new PosworongfulException("pos位置不合法");
        }
        this.elem[pos] = value;

    }

      System.out.println("---------");
      myArrayList.set(0,100);
      myArrayList.disPlay();

9.删除第一次出现的关键字key

空表没有key

先找到key,找到下标

挪动数据

usedSize–

public void remove(int key) {
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
        int index = this.indexOf(key);
        if(index==-1){
            System.out.println("没有这个数字");
        }
        for (int i = index; i < size()-1; i++) {
                this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

        System.out.println("---------");
        myArrayList.remove(3);
        myArrayList.disPlay();

10.清空顺序表

此处顺序表元素不是引用类型,可直接将usedSize变为0来清空顺序表,如果是引用类型,则必须全部置空,否则会造成内存泄漏,空间无法回收

    public void clear() { 
        this.usedSize = 0;
    }

" 本期的分享就到这里了, 记得给博主一个三连哈,你的支持是我创作的最大动力!


原文链接: https://blog.csdn.net/chenchenchencl/article/details/126892216