当前位置: 首页 > news >正文

java实现顺序表

在这里插入图片描述

文章目录

  • 一、顺序表是什么?
  • 二、自定义异常
    • 空引用异常
    • 下标越界异常
  • 三、顺序表的方法
    • 顺序表的实现
    • 获取顺序表长度
    • 顺序表是否为空
    • 顺序表是否为满
    • 打印顺序表
    • 末尾新增元素
    • 指定位置新增元素
    • 判断是否包含某元素
    • 查找某个元素对应的位置
    • 获取 pos 位置的元素
    • 给 pos 位置的元素赋值
    • 删除第一次出现的关键字key
    • 清空顺序表
  • 四、自定义顺序表

一、顺序表是什么?

顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。在数组上完成数据的增删查改。
在这里插入图片描述
数组不就是一个现场的顺序表吗?但是数组并没有直接向我们提供增删查改的工具,所以我们必须重新实现一下顺序表。

二、自定义异常

空引用异常

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

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 - 10; 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 - 10; 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;
    }
}

相关文章:

  • 【英语:基础进阶_核心词汇扩充】E5.常见词根拓词
  • 命令执行漏洞——系统命令执行
  • 【数据结构与算法】List接口栈队列
  • 将cookie字符串转成editthiscookie插件的json格式
  • SpringAOP总结
  • python--数据容器--列表
  • Roson的Qt之旅 #119 QNetworkAddressEntry详细介绍
  • Mybatis -- 使用
  • C语言双链表,循环链表,静态链表讲解(王道版)
  • 比较zab、paxos和raft的算法的异同
  • Python Argparse 库讲解特别好的
  • C++~从编译链接的过程看为什么C++支持重载?externC有什么用?
  • App移动端测试【10】Monkey自定义脚本案例
  • springboot 整合dubbo3开发rest应用
  • 【机器学习】集成学习:使用scikitLearn中的BaggingClassifier实现bagging和pasting策略
  • ES6指北【2】—— 箭头函数
  • SegmentFault for Android 3.0 发布
  • “寒冬”下的金三银四跳槽季来了,帮你客观分析一下局面
  • 11111111
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • Angular Elements 及其运作原理
  • Fabric架构演变之路
  • Hibernate最全面试题
  • Idea+maven+scala构建包并在spark on yarn 运行
  • Java基本数据类型之Number
  • JDK9: 集成 Jshell 和 Maven 项目.
  • js递归,无限分级树形折叠菜单
  • Rancher-k8s加速安装文档
  • vue脚手架vue-cli
  • Vue小说阅读器(仿追书神器)
  • 阿里研究院入选中国企业智库系统影响力榜
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 前端
  • 什么软件可以剪辑音乐?
  • 王永庆:技术创新改变教育未来
  • 在Unity中实现一个简单的消息管理器
  • 宾利慕尚创始人典藏版国内首秀,2025年前实现全系车型电动化 | 2019上海车展 ...
  • 函数计算新功能-----支持C#函数
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • #162 (Div. 2)
  • #Linux(make工具和makefile文件以及makefile语法)
  • #我与Java虚拟机的故事#连载08:书读百遍其义自见
  • (003)SlickEdit Unity的补全
  • (3)选择元素——(17)练习(Exercises)
  • (libusb) usb口自动刷新
  • (SpringBoot)第二章:Spring创建和使用
  • (第27天)Oracle 数据泵转换分区表
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (算法)求1到1亿间的质数或素数
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • (转)自己动手搭建Nginx+memcache+xdebug+php运行环境绿色版 For windows版
  • (转载)CentOS查看系统信息|CentOS查看命令
  • .bat文件调用java类的main方法
  • .naturalWidth 和naturalHeight属性,