Java实现顺序表的操作详解

 更新时间:2022年09月19日 11:29:01   作者:熬夜磕代码丶  
顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。本文主要介绍了顺序表的实现与常用操作,需要的可以参考一下

一、顺序表是什么

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

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

二、自定义异常

空引用异常

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

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顺序表的资料请关注脚本之家其它相关文章!

相关文章

  • Java NIO和IO的区别

    Java NIO和IO的区别

    这篇文章主要介绍了Java NIO和IO的区别,需要的朋友可以参考下
    2014-06-06
  • java教程之java程序编译运行图解(java程序运行)

    java教程之java程序编译运行图解(java程序运行)

    最近重新复习了一下java基础,在使用javap的过程中遇到了一些问题,这里便讲讲对于一个类文件如何编译、运行、反编译的。也让自己加深一下印象
    2014-03-03
  • Java中while语句的简单知识及应用

    Java中while语句的简单知识及应用

    这篇文章主要给大家介绍了关于Java中while语句的简单知识及应用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • Java垃圾回收器的方法和原理总结

    Java垃圾回收器的方法和原理总结

    本篇文章主要介绍了Java垃圾回收器的方法和原理总结,Java垃圾回收器是Java虚拟机的重要模块,具有一定的参考价值,有兴趣的可以了解一下。
    2016-12-12
  • 浅谈spring中用到的设计模式及应用场景

    浅谈spring中用到的设计模式及应用场景

    下面小编就为大家带来一篇浅谈spring中用到的设计模式及应用场景。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • 阿里的一道Java并发面试题详解

    阿里的一道Java并发面试题详解

    这篇文章主要介绍了阿里的一道Java并发面试题详解,网络、并发相关的知识,相对其他一些编程知识点更难一些,主要是不好调试并且涉及内容太多 !,需要的朋友可以参考下
    2019-06-06
  • Java实现4种微信抢红包算法(小结)

    Java实现4种微信抢红包算法(小结)

    微信红包是大家经常使用的,到现在为止仍然有很多红包开发的需求,实现抢红包算法也是面试常考题,本文就详细的来介绍一下如何实现,感兴趣的可以了解一下
    2021-12-12
  • springBoot集成redis的key,value序列化的相关问题

    springBoot集成redis的key,value序列化的相关问题

    这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • springboot时间格式化的五种方法总结(解决后端传给前端的时间显示不一致)

    springboot时间格式化的五种方法总结(解决后端传给前端的时间显示不一致)

    这篇文章主要给大家介绍了关于springboot时间格式化的五种方法,文中介绍的方法解决了后端传给前端的时间显示不一致,文中通过图文以及代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • SpringBoot中的文件上传和异常处理详解

    SpringBoot中的文件上传和异常处理详解

    这篇文章主要介绍了SpringBoot中的文件上传和异常处理详解,对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息,对于浏览器客户端,响应一个"whitelabel"错误视图,以HTML格式呈现相同的数据,需要的朋友可以参考下
    2023-09-09

最新评论