Java实现顺序表的操作详解

2023-05-16 0 1,569

目录

一、顺序表是什么

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

Java实现顺序表的操作详解

数组不就是一个现场的顺序表吗?但是数组并没有直接向我们提供增删查改的工具,所以我们必须重新实现一下顺序表。

二、自定义异常

空引用异常

如果我们的顺序表为空时,手动抛出空引用异常

public class NullException extends RuntimeException{
    public NullException(String message) {
        super(message);
    }
}

下标越界异常

当我们进行增删查改时,下标越界时,我们手动抛出一个下标越界异常

public class IndexException extends RuntimeException{
    public IndexException(String message) {
        super(message);
    }
}

三、顺序表的方法

顺序表的实现

这里我们定义一个顺序表,默认容量为DEFAULTSIZE,实际大小为usedsize.

public class ArrList {
    public int[] arr;
    public int usedSize;
    public static final int DEFAULTSIZE = 10;

    public ArrList() {
        this.arr = new int[DEFAULTSIZE];
    }
}

获取顺序表长度

usedSize存储的就是当前顺序表的长度,直接返回即可。

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

顺序表是否为空

此方法我们只想在顺序表内部使用,所以我们定义为private.

private boolean isEmpty() {
        return this.arr == null;
    }

顺序表是否为满

此方法我们只想在顺序表内部使用,所以我们定义为private.

private boolean isFull() {
        //如果数组所放元素大于等于数组长度,那么数组满了
        return this.size() >= this.arr.length;
    }

打印顺序表

public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(arr[i]+\" \");
        }
        System.out.println();
    }

末尾新增元素

public void add(int data) throws NullException{
        //1.数组为空,报空异常
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.数组满了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.进行新增
        this.arr[this.usedSize] = data;
        //4.元素+1
        this.usedSize++;
    }

指定位置新增元素

public void add(int pos, int data) throws RuntimeException,IndexException{
        //1.判断数组是否为空
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.判断新增位置是否合法,抛数组越界异常
        if(pos < 0 || pos > this.arr.length) {
            throw new IndexException(\"数组越界\");
        }
        //3.判断数组是否已满,进行扩容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.进行新增
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.arr[i+1] = this.arr[i];
        }
        this.arr[pos] = data;
        this.usedSize++;
    }

判断是否包含某元素

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

查找某个元素对应的位置

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

获取 pos 位置的元素

 public int get(int pos) throws IndexException{
        //判断pos位置是否合法
        if(pos < 0 || pos >= this.usedSize) {
            throw new IndexException(\"输入pos位置数组越界\");
        }else {
            return this.arr[pos];
        }
    }

给 pos 位置的元素赋值

public void set(int pos, int value) throws NullException,IndexException{
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.判断新增位置是否合法,抛数组越界异常
        if(pos < 0 || pos >= this.arr.length) {
            throw new IndexException(\"数组越界\");
        }
        this.arr[pos] = value;
    }

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

public void remove(int toRemove) throws NullException{
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        int ret = indexOf(toRemove);
        if(ret == -1) {
            System.out.println(\"不存在此数\");
            return;
        }
        if(ret != -1) {
            for (int i = ret; i < this.usedSize - 1; i++) {
                this.arr[i] = this.arr[i+1];
            }
        }
        this.usedSize++;
    }

清空顺序表

   public void clear() {
        this.usedSize = 0;
        //如果为引用类型
//        for (int i = 0; i < size(); i++) {
//            this.arr[i] = null;
//        }
//        this.usedSize = 0;
    }
}

四、自定义顺序表

public class ArrList {
    public int[] arr;
    public int usedSize;
    public static final int DEFAULTSIZE = 10;

    public ArrList() {
        this.arr = new int[DEFAULTSIZE];
    }
    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(arr[i]+\" \");
        }
        System.out.println();
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) throws NullException{
        //1.数组为空,报空异常
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.数组满了,先增容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //3.进行新增
        this.arr[this.usedSize] = data;
        //4.元素+1
        this.usedSize++;
    }
    private boolean isFull() {
        //如果数组所放元素大于等于数组长度,那么数组满了
        return this.size() >= this.arr.length;
    }
   private boolean isEmpty() {
        return this.arr == null;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) throws RuntimeException,IndexException{
        //1.判断数组是否为空
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.判断新增位置是否合法,抛数组越界异常
        if(pos < 0 || pos > this.arr.length) {
            throw new IndexException(\"数组越界\");
        }
        //3.判断数组是否已满,进行扩容
        if(isFull()) {
            this.arr = new int[2 * this.arr.length];
        }
        //4.进行新增
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.arr[i+1] = this.arr[i];
        }
        this.arr[pos] = data;
        this.usedSize++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.arr[i]) {
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) throws IndexException{
        //判断pos位置是否合法
        if(pos < 0 || pos >= this.usedSize) {
            throw new IndexException(\"输入pos位置数组越界\");
        }else {
            return this.arr[pos];
        }
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) throws NullException,IndexException{
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        //2.判断新增位置是否合法,抛数组越界异常
        if(pos < 0 || pos >= this.arr.length) {
            throw new IndexException(\"数组越界\");
        }
        this.arr[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) throws NullException{
        if(isEmpty()) {
            throw new NullException(\"数组为空\");
        }
        int ret = indexOf(toRemove);
        if(ret == -1) {
            System.out.println(\"不存在此数\");
            return;
        }
        if(ret != -1) {
            for (int i = ret; i < this.usedSize - 1; i++) {
                this.arr[i] = this.arr[i+1];
            }
        }
        this.usedSize++;
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
        //如果为引用类型
//        for (int i = 0; i < size(); i++) {
//            this.arr[i] = null;
//        }
//        this.usedSize = 0;
    }
}

以上就是Java实现顺序表的操作详解的详细内容,更多关于Java顺序表的资料请关注其它相关文章!

资源下载此资源下载价格为1小猪币,终身VIP免费,请先
由于本站资源来源于互联网,以研究交流为目的,所有仅供大家参考、学习,不存在任何商业目的与商业用途,如资源存在BUG以及其他任何问题,请自行解决,本站不提供技术服务! 由于资源为虚拟可复制性,下载后不予退积分和退款,谢谢您的支持!如遇到失效或错误的下载链接请联系客服QQ:442469558

:本文采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可, 转载请附上原文出处链接。
1、本站提供的源码不保证资源的完整性以及安全性,不附带任何技术服务!
2、本站提供的模板、软件工具等其他资源,均不包含技术服务,请大家谅解!
3、本站提供的资源仅供下载者参考学习,请勿用于任何商业用途,请24小时内删除!
4、如需商用,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
5、本站部分资源存放于百度网盘或其他网盘中,请提前注册好百度网盘账号,下载安装百度网盘客户端或其他网盘客户端进行下载;
6、本站部分资源文件是经压缩后的,请下载后安装解压软件,推荐使用WinRAR和7-Zip解压软件。
7、如果本站提供的资源侵犯到了您的权益,请邮件联系: 442469558@qq.com 进行处理!

猪小侠源码-最新源码下载平台 Java教程 Java实现顺序表的操作详解 https://www.20zxx.cn/705652/xuexijiaocheng/javajc.html

猪小侠源码,优质资源分享网

常见问题
  • 本站所有资源版权均属于原作者所有,均只能用于参考学习,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,建议提前注册好百度网盘账号,使用百度网盘客户端下载
查看详情

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务