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

相关文章

  • 你知道jdk竟有4个random吗

    你知道jdk竟有4个random吗

    这篇文章主要给大家介绍了关于jdk中4个random的相关资料,分别是Random、ThreadLocalRandom、SecureRandom以及SplittableRandom,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2021-06-06
  • java实现单机版五子棋

    java实现单机版五子棋

    这篇文章主要为大家详细介绍了java实现单机版五子棋源码,以及五子棋游戏需要的实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-03-03
  • Java8中对泛型目标类型推断方法的改进

    Java8中对泛型目标类型推断方法的改进

    这篇文章主要介绍了Java8中对泛型目标类型推断方法的改进,需要的朋友可以参考下
    2014-06-06
  • 深入浅析HashMap key和value能否为null

    深入浅析HashMap key和value能否为null

    HashMap的key和value可为null,线程不安全,HashTable的key和value均不可为null,线程安全,ConcurrentHashMap在多线程场景下使用,key和value也不能为null,还对它们进行了测试和底层代码分析,本文介绍HashMap key和value能否为null,感兴趣的朋友跟随小编一起看看吧
    2025-04-04
  • 如何使用nexus在局域网内搭建maven私服及idea的使用

    如何使用nexus在局域网内搭建maven私服及idea的使用

    这篇文章主要介绍了如何使用nexus在局域网内搭建maven私服及idea的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • SpringMVC拦截器详解

    SpringMVC拦截器详解

    本篇文章主要介绍了SpringMVC拦截器配置及使用方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • Java Swing中的表格(JTable)和树(JTree)组件使用实例

    Java Swing中的表格(JTable)和树(JTree)组件使用实例

    这篇文章主要介绍了Java Swing中的表格(JTable)和树(JTree)组件使用实例,本文同时讲解了表格和树的基本概念、常用方法、代码实例,需要的朋友可以参考下
    2014-10-10
  • 浅谈JackSon的几种用法

    浅谈JackSon的几种用法

    这篇文章主要介绍了浅谈JackSon的几种用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-01-01
  • JAVA加密算法数字签名实现原理详解

    JAVA加密算法数字签名实现原理详解

    这篇文章主要介绍了JAVA加密算法数字签名实现原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-10-10
  • springMVC实现文件上传和下载

    springMVC实现文件上传和下载

    这篇文章主要为大家详细介绍了springMVC实现文件上传和下载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08

最新评论