• Java数据结构——代码实现顺序表的操作


    ced485cbb11e458d81a746890b32cf3f.gif

     作者:敲代码の流川枫

    博客主页:流川枫的博客

    专栏:和我一起学java

    语录:Stay hungry stay foolish

    工欲善其事必先利其器,给大家介绍一款超牛的斩获大厂offer利器——牛客网

    点击免费注册和我一起刷题吧

    文章目录

    顺序表

    1.获取顺序表长度

    2.打印顺序表中的所有元素

    3.新增一个元素,默认在最后新增

    4.在 pos 位置新增元素

    5.判定是否包含某个元素

    6.查找某个元素对应的位置

    7.获取 pos 位置的元素

    8.给 pos 位置的元素更新为 value

    9.删除第一次出现的关键字key

    10.清空顺序表


    顺序表

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

    接下来我们自己实现一些接口对数组进行增删查改的操作

     创建一个类:

    1. public class MyArrayList {
    2. public int[] elem;//数组
    3. public int usedSize;//记录有效数据的个数
    4. public static final int DEFAULT_SIZE = 10;
    5. public MyArrayList(int[] elem){
    6. this.elem = new int[DEFAULT_SIZE];
    7. }

    1.获取顺序表长度

    1. public int size() {
    2. return this.usedSize;
    3. }

    2.打印顺序表中的所有元素

    1. public void disPlay(){
    2. for (int i = 0; i < this.usedSize; i++) {
    3. System.out.println(this.elem[i]+" ");
    4. }
    5. System.out.println();
    6. }

    3.新增一个元素,默认在最后新增

    步骤:检查当前顺序表是否满了;如果满了进行扩容;然后将元素放进去;usedSize++;

    判满:

    1. public boolean isFull(int[] elem){
    2. if(size()>=this.usedSize){
    3. return true;
    4. }
    5. else return false;
    6. }

    扩容:

    1. public void add(int data){
    2. if(isFull()){
    3. this.elem =
    4. Arrays.copyOf(this.elem,2*this.elem.length);
    5. }
    6. //添加数据
    7. this.elem[this.usedSize] = data;
    8. //有效数据加一
    9. this.usedSize++;
    10. }

    测试:

    1. public class TestList {
    2. public static void main(String[] args) {
    3. MyArrayList myArrayList = new MyArrayList();
    4. myArrayList.add(1);
    5. myArrayList.add(2);
    6. myArrayList.add(3);
    7. myArrayList.disPlay();
    8. }
    9. }

    4.在 pos 位置新增元素

    注意:负数下标不能当Pos;不能超过数组长度插入;不能间隔着插入,即插入的位置前面一定有元素;

    还是先判断数组是否满,满了扩容;然后判断pos位置是否合法;不合法抛出异常;插入后将其他元素后移

    1. public void add(int pos, int data) {
    2. if(isFull()){
    3. System.out.println("满了");
    4. this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    5. }
    6. if(pos<0||pos>this.usedSize){
    7. System.out.println("pos位置不合法");
    8. throw new PosworongfulException("pos位置不合法");
    9. }
    10. //pos一定合法
    11. // 挪动数据
    12. for (int i = this.usedSize-1; i>=pos ; i--) {
    13. this.elem[i+1] = this.elem[i];
    14. }
    15. //插入数据
    16. this.elem[pos] = data;
    17. //usedSize++
    18. this.usedSize++;
    19. }

    添加10到1位置 

    1. try {
    2. myArrayList.add(1,10);
    3. }
    4. catch (PosworongfulException e){
    5. e.printStackTrace();
    6. }
    7. myArrayList.disPlay();

     在10位置添加数据

    1. try {
    2. myArrayList.add(10,10);
    3. }
    4. catch (PosworongfulException e){
    5. e.printStackTrace();
    6. }
    7. myArrayList.disPlay();

    5. 判定是否包含某个元素

    1. public boolean contains(int toFind) {
    2. for (int i = 0; i < this.size(); i++) {
    3. if(elem[i]==toFind){
    4. return true;
    5. }
    6. }
    7. return false;
    8. }

    1. System.out.println("-------------");
    2. System.out.println(myArrayList.contains(10));
    3. System.out.println(myArrayList.contains(100));

    6.查找某个元素对应的位置

    1. public int indexOf(int toFind) {
    2. for (int i = 0; i < this.size(); i++) {
    3. if(this.elem[i]==toFind){
    4. return i;
    5. }
    6. }
    7. return -1;
    8. }
    1. System.out.println("-------------");
    2. System.out.println(myArrayList.indexOf(3));

    7.获取 pos 位置的元素

    1. //判断为空
    2. public boolean isEmpty(){
    3. return size()==0;
    4. }
    5. // 获取 pos 位置的元素
    6. public int get(int pos) {
    7. if(isEmpty()){
    8. throw new EmptyException("当前顺序表为空");
    9. }
    10. if(pos<0||pos>=this.usedSize){
    11. throw new PosworongfulException("pos位置不合法");
    12. }
    13. return this.elem[pos];
    14. }
    1. System.out.println("------------");
    2. System.out.println(myArrayList.get(0));
    3. try {
    4. myArrayList.get(10);
    5. }catch (PosworongfulException e){
    6. e.printStackTrace();
    7. }

    8.给 pos 位置的元素更新为 value

    1. public void set(int pos, int value) {
    2. if(isEmpty()){
    3. throw new EmptyException("顺序表为空");
    4. }
    5. if(pos<0||pos>=this.usedSize){
    6. throw new PosworongfulException("pos位置不合法");
    7. }
    8. this.elem[pos] = value;
    9. }
    1. System.out.println("---------");
    2. myArrayList.set(0,100);
    3. myArrayList.disPlay();

    9.删除第一次出现的关键字key

    空表没有key

    先找到key,找到下标

    挪动数据

    usedSize--

    1. public void remove(int key) {
    2. if(isEmpty()){
    3. throw new EmptyException("顺序表为空");
    4. }
    5. int index = this.indexOf(key);
    6. if(index==-1){
    7. System.out.println("没有这个数字");
    8. }
    9. for (int i = index; i < size()-1; i++) {
    10. this.elem[i] = this.elem[i+1];
    11. }
    12. this.usedSize--;
    13. }
    1. System.out.println("---------");
    2. myArrayList.remove(3);
    3. myArrayList.disPlay();

    10.清空顺序表

    此处顺序表元素不是引用类型,可直接将usedSize变为0来清空顺序表,如果是引用类型,则必须全部置空,否则会造成内存泄漏,空间无法回收

    1. public void clear() {
    2. this.usedSize = 0;
    3. }

    “ 本期的分享就到这里了, 记得给博主一个三连哈,你的支持是我创作的最大动力!

    ced485cbb11e458d81a746890b32cf3f.gif

  • 相关阅读:
    关于硬盘质量大数据分析的思考
    解决线程安全问题&&单例模式
    doxygen c++ 语法
    vue使用百度地图(vue-baidu-map)
    港陆证券:日线9连阴意味着什么?
    二、SpringBoot自动装配及SPI的理解
    MacBook/MacOS如何更新到指定的版本
    所有的注意力不专注都是目标不明确、不坚定的表现
    Spring Boot集成Easypoi导出Excel
    ECA-Net(Efficient Channel Attention Network)
  • 原文地址:https://blog.csdn.net/chenchenchencl/article/details/126892216