数据结构与算法之顺序表篇(1)

目录

1. 顺序表

1.1 打印顺序表

1.2 新增元素,默认在数组最后新增

1.3 在 pos 位置新增元素

 1.4 判定是否包含某个元素

1.5 查找某个元素对应的位置

1.6 获取 pos 位置的元素

1.7 给 pos 位置的元素设为 value

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

 1.9 获取顺序表长度

 1.10 清空顺序表


1. 顺序表

顺序表是用一段 物理地址连续 的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成 数据的增删查改。
public class SeqList {
// 打印顺序表
public void display() { }
// 新增元素,默认在数组最后新增
public void add(int data) { }
// 在 pos 位置新增元素
public void add(int pos, int data) { }
// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
// 查找某个元素对应的位置
public int indexOf(int toFind) { return -1; }
// 获取 pos 位置的元素
public int get(int pos) { return -1; }
// 给 pos 位置的元素设为 value
public void set(int pos, int value) { }
//删除第一次出现的关键字key
public void remove(int toRemove) { }
// 获取顺序表长度
public int size() { return 0; }
// 清空顺序表
public void clear() { }
}

那么对于一个顺序表而言,就有上面的几种方法,那么这些方法在我们的顺序表中有已经写好的代码,但是我们具体来讲解以下这些方法的具体实现

1.1 打印顺序表

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

1.2 新增元素,默认在数组最后新增

要分析情况,看看我们的数组空间是否足够

// 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull()) {//如果数组的空间够
            this.arr[usiedsize] = data;
        }
        else {//如果我们的数组的空间不够,那么我们就需要去扩容之后,再去给他存放
            this.arr = Arrays.copyOf(this.arr,this.arr.length*2);
            this.arr[usiedsize] = data;
        }
        usiedsize++;
    }
    public boolean isFull() {
        return this.arr.length > usiedsize;
    }

1.3 在 pos 位置新增元素

    public void add(int pos, int data) {
        if (isFull()) {//满了就扩容
            System.out.println("满了!");
            this.arr = Arrays.copyOf(this.arr,this.arr.length*2);
        }
        //如果没有满那么就需要考虑pos的合法性
        if (pos >= this.arr.length || pos < 0) {
            System.out.println("pos不合法!");
            //不合法我们就去捕捉这个异常
            throw new PosWrongfulException("pos位置不合法!");
        }
        //此时就要进行添加
        for (int i = usiedsize; i > pos ; i--) {
            this.arr[i] = this.arr[i-1];
        }
        this.arr[pos] = data;
        this.usiedsize++;
    }

抛出异常

public class PosWrongfulException extends RuntimeException{
    public PosWrongfulException() {
    }

    public PosWrongfulException(String message) {
        super(message);
    }
}

用try catch 语句捕捉这个异常


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

下面是我们的分析过程:

 1.4 判定是否包含某个元素

这就是一个数组的遍历,看这个数组中是否有这个元素,如果有就返回ture 如果没有就返回false.

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

1.5 查找某个元素对应的位置

public int indexOf(int toFind) {//如果有就返回下标,没有就返回-1
        for (int i = 0; i < this.usiedsize; i++) {
            if (arr[i] == toFind){
                return i;
            }
        }
        return -1;
    }

1.6 获取 pos 位置的元素

public int get(int pos) {
        if (isNull()){//如果数组是空
             throw new EmptyException("数组为空值!");
        }
        if (pos >= this.arr.length || pos < 0) {//如果pos的位置不合法
            System.out.println("pos不合法!");
            throw new RuntimeException("get获取元素的时候,pos不合法!");
        }
        return this.arr[pos];
    }

抛出异常

public class EmptyException extends RuntimeException{
    public EmptyException() {
    }

    public EmptyException(String message) {
        super(message);
    }
}


public class PosWrongfulException extends RuntimeException{
    public PosWrongfulException() {
    }

    public PosWrongfulException(String message) {
        super(message);
    }
}

用try catch 语句捕捉这个异常

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

        try {
            System.out.println(myArrayList.get(1));
        }catch (PosWrongfulException e){
            e.printStackTrace();
        }

下面就是我们的分析过程:

1.7 给 pos 位置的元素设为 value

   public void set(int pos, int value) {
        if (isNull()){//如果数组是空
            throw new EmptyException("数组为空值!");
        }
        if (pos >= this.arr.length || pos < 0) {//如果pos的位置不合法
            System.out.println("pos不合法!");
            throw new RuntimeException("get获取元素的时候,pos不合法!");
        }
        this.arr[pos] = value;
    }

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

    public void remove(int toRemove) {
        if (isNull()){//如果数组是空
            throw new EmptyException("数组为空值!");
        }
        int index = indexOf(toRemove);
        if (index == -1){
            System.out.println("没有这个元素");
        }
        for (int i = index; i < usedsize-1; i++) {
            this.arr[i] = this.arr[i+1];
        }
        usedsize --;
    }

分析过程如下:

 1.9 获取顺序表长度

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

 1.10 清空顺序表

public void clear() {
//如果是引用的话,就可以让数组中的所有元素的值变成null
       // for (int i = 0; i < usedsize; i++) {
       //     this.arr[i] = null;
       // }
//我们的数组是纯数字的,可以让usedsize变为0,数组就为空了.
        this.usedsize = 0;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java小白~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值