超详细!JAVA实现顺序表类

2023-05-16

Seqlist类


/*
增、删、改、查,判断是否为空
 */

public class Seqlist<T> {

    protected int n;//顺序表元素个数
    protected Object[] element;//顺序表元素

    public static final int MIN_CAPCITY = 16;//顺序表最小容量


    //构造函数指定了长度
    public Seqlist(int lenth) {
        if (lenth < MIN_CAPCITY) {
            lenth = MIN_CAPCITY;
        }
        //System.out.println("已经调用");
        this.element = new Object[lenth];
        this.n = 0;

    }

    //构造函数什么都未指定,默认长度
    //如果未指定长度,this方法自动调用其他构造方法
    public Seqlist() {

        this(MIN_CAPCITY);

    }

    //构造函数指定数组
    public Seqlist(T[] values) {

        this(values.length * 2);//this方法调用指定长度的构造方法
        for (int i = 0; i < values.length; i++) {
            if (values[i] != null) {
                this.element[this.n++] = values[i];
            }
        }
    }


    /*
     public boolean isEmpty()
     判断是否为空
     return:  是否为空
     */
    public boolean isEmpty() {
        return (this.n == 0);
    }


    /*
    public T get(int i)
    指定元素,返回元素
    i:要返回元素的索引
     */
    public T get(int i) {

        if (i >= 0 && i <= n) {
            return (T) this.element[i];
        }

        return null;
    }


    /*
    public void set(int i,T x)
    指定元素进行赋值
    i:指定元素位置索引
    x:元素的值
     */
    public void set(int i, T x) {

        if (x == null) {
            throw new NullPointerException("X=NULL");
        }

        if (i >= 0 && i <= this.n) {
            element[i] = x;
        } else {
            throw new ArrayIndexOutOfBoundsException(i + "");
        }

    }


    /*
     public int getSize()
     返回顺序表长度
     return:返回元素的长度,如果没有则返回-1
     */
    public int getSize() {
        if (this.n >= 0) {
            return this.n;
        }

        return -1;
        //return null;
    }

    /*
    public int insert(int k, T e)
    指定索引,插入元素
    k:指定索引
    e:要插入的元素
    return;返回插入的索引
     */
    public int insert(int k, T e) {
        if (e == null) {return -1;}// 为空则return-1
        if (k < 0) {k = 0;} //如果小于0,就查到头部
        if (k >this.n) {k = this.n;} //如果要插入的位置大于当前数组元素数量,就插到尾部

        //数组扩容
        //如果没有扩容,source就是创建了一个相同的数组,为下面的插入进行准备
        //如果做了扩容,也是一样
        Object[] source = this.element;//如果不扩容,操作source和element都一样
        if(this.n == this.element.length){
            this.element = new Object[source.length*2];//进行扩容
            for (int j = 0;  j < this.n; j++) {
                element[j] = source[j];//进行赋值
            }
        }

        //进行插入
        //利用source进行插入
        int m = 0;
        for (int j = this.n - 1; j > k; j--) {
            this.element[j+1] = source[j];
        }
        this.element[k] = e;
        this.n++;


        return k;
    }

    /*
    public T delete(int index)
    删除,并返回删除的元素
    index:要删除元素的索引
    return:被删除的元素
     */
    public T delete(int index){

        //没有元素,无发删除
        //大于存储的元素数量无法删除
        //负数索引不行
        if( n == 0 || index >n || index < 0){
            return null;
        }

        //存储要删除的元素,作为返回值
        T temp = (T)element[index];

        //如果是删除尾部元素,直接删除
        if( index == n ){
             this.element[index] = null;
        }

        //进行删除
        for (int k = index; k < this.n - 1; k++){
            //T temp;
            element[k] = element[k + 1];
        }
        element[this.n-1] = null;
        this.n--;
        return temp;
    }


    /*
    public void clear()
    清空所有元素
     */
    public void clear(){ this.n = 0;}

    /*
    public int search(T key)
    进行查找
    key;要查找的元素
    return: 如果找到该元素,则返回该元素索引
    否则,返回-1,为没有找到该元素
     */

    public int search(T key){

        for (int i = 0; i < this.n; i++) {

            if(key.equals(this.element[i])){
                return i;
            }
        }
        return -1;


    }
    @Override
    public String toString() {
        String str = this.getClass().getName() + "(";
        if (this.n > 0) {
            str += this.element[0].toString();
        }

        for (int i = 1; i < this.n; i++) {
            str += "," + this.element[i].toString();
        }
        return str + ")";
    }
}

Test 类进行测试

public class Test{
    public static void main(String[] args) {
        Integer [] str = {1,2,3,4};
        Seqlist seqlist = new Seqlist<>(str);

        System.out.println(seqlist.delete(1));//进行删除
        System.out.println(seqlist);
    }




}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

超详细!JAVA实现顺序表类 的相关文章

随机推荐

  • 【Docker学习笔记】2.Debian Docker 安装及CentOS Docker 安装

    前言 本章介绍Debian Docker 安装和CentOS Docker 安装 Debian Docker 安装 Docker 支持以下的 Debian 版本 xff1a 巴斯特 10拉伸 9 xff08 稳定 xff09 拉斯比亚拉伸
  • 超详细的操作符讲解

    操作符的分类 算术操作符 移位操作符 位操作符 赋值操作符 单目操作符 关系操作符 逻辑操作符 条件操作符 逗号表达式 1 算术操作符 span class token operator 43 span span class token o
  • Ubuntu 安装中文输入法

    请注意命令中不应该的空格可能导致命令不合法 xff01 一 检查 fcitx 框架 首先 xff0c 要安装中文输入法 xff0c 必须要保证系统上有 fcitx fcitx是一个以 GPL 方式发布的输入法框架 xff0c 安装 fcit
  • 快速幂取模简单用法

    关于快速幂的用法本小白还是思索了挺久的 xff08 因为我不太聪明哈 xff09 xff0c 但是学会了就觉得挺好理解的 下面我用一道例题来简单讲一下它的用法 xff0c 希望能帮你轻松get到 xff01 xff01 例题 xff1a 快
  • robomaster视觉规则细谈

    目录 攻击与检测 弹丸参数 增益点增益 升级效果 击打检测 涂装要求 裁判系统 机器人端各模块 赛事引擎各部分 客户端 服务器 能量机关 小能量机关 大能量机关 算法归纳 攻击与检测 弹丸参数 如图所示 xff0c 赛场中我们使用的弹丸有两
  • 关于如何用python下载文件

    先贴上源代码 xff1a GitHub zhangbaji PythonDownloader 这是一个Python下载http文件的事例 xff0c 只不过目前还无法获取动态文件的文件名 https github com zhangbaji
  • Java打印杨辉三角形

    span class token keyword public span span class token keyword class span span class token class name Taks02 span span cl
  • 第二章 利用ffmpeg把rgb转mp4

    本文章介绍ffmpeg基本使用流程 1 创建编码器 通过枚举id选择编码器类型 AVCodec avcodec find encoder enum AVCodecID id enum AVCodecID id 通过枚举选择编码器类型 AV
  • 栈的相关题目以及出栈时的规律

    今天做了两个有关栈的题目 xff0c 都可以用到出栈时的规律来解题 那么问题来了 xff0c 出栈时的规律是什么呢 xff1f 规律如下 xff1a 已知栈的输入序列是1 2 3 n xff0c 输出序列是a1 a2 ai an 然后我们任
  • Python 中 jieba 库

    文章目录 jieba库一 简介1 是什么2 安装 二 基本使用1 三种模式2 使用语法2 1 对词组的基本操作2 2 关键字提取2 3 词性标注2 4 返回词语在原文的起止位置 jieba库 一 简介 1 是什么 xff08 1 xff09
  • 【栈与队列】之栈的顺序存储(图文详细介绍!!)

    前言 xff1a 本章基于 大话数据结构 和王卓老师的视频内容 xff0c 为刚接触数据结构的初学者提供一些帮助 x1f495 如果我的文章对你有帮助 xff0c 点赞 收藏 留言都是对我最大的动力 目录 4 1 栈的定义 4 2 栈的抽象
  • 【C语言技能树】程序环境和预处理

    Halo xff0c 这里是Ppeua 平时主要更新C语言 xff0c C 43 43 xff0c 数据结构算法 感兴趣就关注我吧 xff01 你定不会失望 x1f308 个人主页 xff1a 主页链接 x1f308 算法专栏 xff1a
  • Java中输出所有的水仙花数

    问题描述 打印出所有的 水仙花数 xff0c 所谓 水仙花数 是指一个三位数 xff0c 其各位数字立方和等于该数本身 例如 xff1a 153是一个 水仙花数 xff0c 因为153 61 1的三次方 43 5的三次方 43 3的三次方
  • pip3 设置阿里云

    pip3 install r requirements txt 报超时 xff0c 于是设置阿里云作为安装源 xff1a pip3 config set global index url http mirrors aliyun com py
  • 输入一个数组,将其逆序输出

    今天参加了校内的计算机技能大赛 xff0c 找到了一道较为简单的题 xff0c 就是 将数组逆序输出 下面我将详细讲解一下代码思路 xff0c 好了 xff0c 老规矩 xff0c 先上代码 xff01 include lt bits st
  • 虚拟机中Ubuntu安装了anaconda3无法使用conda

    ubuntu 中安装了 anaconda3 但是无法 使用 conda 只会出现这句话 conda 未找到指令 我找了一些办法 xff0c 有一个有用的 xff1a 8条消息 Ubuntu下使用Anaconda3 出现conda 未找到命令
  • ubuntu配置nfs时Failed to start nfs-server.service: Unit nfs-server.service not found.

    在ubuntu系统中配置nfs时出现Failed to start nfs server service Unit nfs server service not found 原因 xff1a 新装的ubuntu系统并未安装nfs 应使用su
  • 【经验分享】使用Keil5烧录代码遇到的问题及解决方法

    目录 一 前言 二 所遇问题及解决方法 1 首先最基本的Options for target 编辑的设置不用多说 xff0c 下载器根据自己所使用的类型进行选择 我使用的是CMSIS DAP 2 第二种可能出现的问题如下 SWD JTAG
  • c++ delete与析构函数的注意点

    问题 xff1a 我们都知道析构函数在类对象作用域结束时自动调用 xff0c 但这个规则适合基本类型 xff0c 但不适合delete函数 原因 xff1a 如果对象是new运算符动态创建的 xff0c 如果最后没有调用delete xff
  • 超详细!JAVA实现顺序表类

    Seqlist类 增 删 改 查 xff0c 判断是否为空 public class Seqlist lt T gt protected int n 顺序表元素个数 protected Object element 顺序表元素 public