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

数据结构之顺序表的模拟实现

 💕"世事犹如书籍,一页页被翻过去。人要向前看,少翻历史旧账。"💕

作者:Mylvzi 

 文章主要内容:数据结构之顺序表的模拟实现 

 

/*** Created with IntelliJ IDEA.* Description:* User: 绿字* Date: 2023-10-12* Time: 8:53*/
import java.util.*;
/*** 顺序表详解*/
public class MyArrayList {private int[] elem;// 存放数据的数组private int usedSize;// 有效数据个数public static final int DEFAULT_SIZE = 10;// 初始化顺序表public MyArrayList() {this.elem = new int[DEFAULT_SIZE];}public MyArrayList(int ininCapacity) {// 自定义数组的大小this.elem = new int[ininCapacity];}// 打印顺序表public void display() {for (int i = 0; i <this.usedSize ; i++) {System.out.print(this.elem[i] +" ");}}// 添加数据  默认是在末尾添加public void add(int data) {// 满了要扩容if(isFull()) {this.elem = Arrays.copyOf(this.elem,2*this.elem.length);}this.elem[usedSize] = data;this.usedSize++;}// 判断是否已满public boolean isFull() {if(this.usedSize == this.elem.length) {return true;}return false;}// 在 pos 位置新增元素public void add(int pos, int data) {// pos位置要合法if(pos<0 || pos>this.usedSize) {throw new RuntimeException(pos+"位置不合法");}if(isFull()) {this.elem = Arrays.copyOf(this.elem,2*this.elem.length);}for (int i = this.usedSize-1; i >=pos ; i--) {this.elem[i+1] = this.elem[i];}this.elem[pos] = data;this.usedSize++;}// 判定是否包含某个元素public boolean contains(int toFind) {for (int i = 0; i <this.usedSize ; i++) {if(this.elem[i] == toFind) {return true;}}System.out.println("数组不包含该元素");return false;}// 查找某个元素对应的位置public int indexOf(int toFind) {for (int i = 0; i <this.usedSize ; i++) {if(this.elem[i] == toFind) {return i;}}System.out.println("数组不包含该元素");return -1;}// 检查pos位置是否合法private void checkPosLegal(int pos) {if(pos<0 || pos>=this.usedSize) {throw new posOutOfBoundException(pos+" 位置不合法");}}// 获取 pos 位置的元素public int get(int pos) {checkPosLegal(pos);return this.elem[pos];}// 给 pos 位置的元素设为 value  pos位置必须含有元素public void set(int pos, int value) {checkPosLegal(pos);this.elem[pos] = value;}//删除第一次出现的关键字keypublic void remove(int toRemove) {int index = indexOf(toRemove);for (int i = index; i <this.usedSize-1 ; i++) {this.elem[i] = this.elem[i+1];}this.usedSize--;}// 获取顺序表长度public int size() {return this.usedSize;}// 清空顺序表public void clear() {// 如果是引用类型所有的引用都要置空
//        for (int i = 0; i <this.usedSize; i++) {
//            this.elem[i] = null;
//        }this.usedSize = 0;}
}
/*** Created with IntelliJ IDEA.* Description:* User: 绿字* Date: 2023-10-12* Time: 10:49*/
public class posOutOfBoundException extends RuntimeException{public posOutOfBoundException() {}public posOutOfBoundException(String message) {super(message);}
}
/*** Created with IntelliJ IDEA.* Description:* User: 绿字* Date: 2023-10-12* Time: 9:02*/
public class Test1 {public static void main(String[] args) {MyArrayList myArrayList = new MyArrayList(5);myArrayList.add(1);myArrayList.add(2);myArrayList.add(3);myArrayList.add(4);
/*        System.out.println(myArrayList.size());myArrayList.remove(1);System.out.println(myArrayList.size());*/
//        myArrayList.add(100,99);
/*        myArrayList.set(0,999);myArrayList.remove(999);myArrayList.remove(2);myArrayList.remove(3);myArrayList.remove(4);myArrayList.remove(5);*//*        System.out.println(myArrayList.get(0));System.out.println(myArrayList.get(-1));System.out.println(myArrayList.get(999));*/
//        System.out.println(myArrayList.contains(2));
//        System.out.println(myArrayList.indexOf(2));
//
/*        myArrayList.add(0,999);myArrayList.add(0,999);myArrayList.add(0,999);myArrayList.add(0,999);myArrayList.add(0,999);*/
myArrayList.clear();myArrayList.display();}
}

http://www.lryc.cn/news/198577.html

相关文章:

  • R6G azide, 5-isomer具有良好的水溶性,2135330-71-9
  • Canvas系列绘制图片学习:绘制图片和渐变效果
  • AJAX为什么叫AJAX
  • 自动化测试中如何编写配置文件 ? 该使用什么工具 ? 一文详解使用ConfigParser读写配置文件
  • 文件批量管理:轻松复制备份并删除原文件
  • Linux高性能服务器编程 学习笔记 第十七章 系统监测工具
  • rabbitmq 消费者报错 ListenerExecutionFailedException NullPointerException
  • Java面试题:链表-合并两个排序的链表
  • Springboot结合Mockito写单元测试实践和原理
  • 操作系统之微内核架构
  • 24---WPF缓存
  • vite+vue3.0 使用tailwindcss
  • C++QT---QT-day3
  • 小程序如何搭建在服务器上
  • JavaEE初阶学习:Servlet
  • 黑白二维码不好看,那么快学习改色的方法吧
  • coreldraw2024版本有哪些新增功能?
  • 2023最新Office2021专业增强版安装使用教程
  • 实时配送跟踪功能的实现:外卖跑腿小程序的技术挑战
  • react实现一维表格、键值对数据表格key value表格
  • 个人微信CRM客户管理系统怎么选?功能介绍
  • Mac Intellij Idea get/set方法快捷键
  • 并发程序设计
  • openGauss学习笔记-104 openGauss 数据库管理-管理数据库安全-客户端接入之SSL证书管理-证书替换
  • react仿照antd progress实现可自定义颜色的直角矩形进度条
  • 【网络安全】被恶意攻击的IP地址有多可怕?
  • Guava-RateLimiter详解
  • 【C++11】右值引用、移动构造、移动赋值、完美转发 的原理介绍
  • Python【理解标识符的定义】
  • AR智能眼镜主板设计方案_AR眼镜PCB板设计