实现一些接口对数组进行增删查改的操作

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

顺序表

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

 创建一个类:

public class MyArrayList {

public int[] elem;//数组
public int usedSize;//记录有效数据的个数
public static final int DEFAULT_SIZE = 10;
public MyArrayList(int[] elem){
    this.elem = new int[DEFAULT_SIZE];
}

1.获取顺序表长度
public int size() {

    return this.usedSize;

}
2.打印顺序表中的所有元素
public void disPlay(){

    for (int i = 0; i < this.usedSize; i++) {
        System.out.println(this.elem[i]+" ");
    }
    System.out.println();

}
3.新增一个元素,默认在最后新增
步骤:检查当前顺序表是否满了;如果满了进行扩容;然后将元素放进去;usedSize++;

判满:

public boolean isFull(int[] elem){

    if(size()>=this.usedSize){
        return true;
    }
    else return false;

}
扩容:

public void add(int data){

    if(isFull()){
        this.elem = 
                Arrays.copyOf(this.elem,2*this.elem.length);
    }
    //添加数据
    this.elem[this.usedSize] = data;
    //有效数据加一
    this.usedSize++;

}
测试:

public class TestList {

public static void main(String[] args) {
    MyArrayList myArrayList = new MyArrayList();
    myArrayList.add(1);
    myArrayList.add(2);
    myArrayList.add(3);
    myArrayList.disPlay();
}

}

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

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

public void add(int pos, int data) {

    if(isFull()){
        System.out.println("满了");
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }
    if(pos<0||pos>this.usedSize){
        System.out.println("pos位置不合法");
        throw new PosworongfulException("pos位置不合法");
    }
    //pos一定合法
    // 挪动数据
    for (int i = this.usedSize-1; i>=pos  ; i--) {
        this.elem[i+1] = this.elem[i];
    }
    //插入数据
    this.elem[pos] = data;
    //usedSize++
    this.usedSize++;

}
添加10到1位置 

try {

            myArrayList.add(1,10);
    }
    catch (PosworongfulException e){
            e.printStackTrace();
    }
            myArrayList.disPlay();

 在10位置添加数据

try {

            myArrayList.add(10,10);
    }
    catch (PosworongfulException e){
            e.printStackTrace();
    }
            myArrayList.disPlay();

5. 判定是否包含某个元素
public boolean contains(int toFind) {

    for (int i = 0; i < this.size(); i++) {
        if(elem[i]==toFind){
            return true;
        }
    }
    return false;

}

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

6.查找某个元素对应的位置
public int indexOf(int toFind) {

    for (int i = 0; i < this.size(); i++) {
        if(this.elem[i]==toFind){
            return i;
        }
    }
    return -1;

}

    System.out.println("-------------");
    System.out.println(myArrayList.indexOf(3));

7.获取 pos 位置的元素
//判断为空

public boolean isEmpty(){
    return size()==0;
}
// 获取 pos 位置的元素
public int get(int pos) {
    if(isEmpty()){
        throw new EmptyException("当前顺序表为空");
    }
    if(pos<0||pos>=this.usedSize){
        throw new PosworongfulException("pos位置不合法");
    }
    return this.elem[pos];
}
System.out.println("------------");
System.out.println(myArrayList.get(0));
 try {
      myArrayList.get(10);
 }catch (PosworongfulException e){
       e.printStackTrace();
 }

8.给 pos 位置的元素更新为 value
public void set(int pos, int value) {

    if(isEmpty()){
        throw new EmptyException("顺序表为空");
    }
    if(pos<0||pos>=this.usedSize){
        throw new PosworongfulException("pos位置不合法");
    }
    this.elem[pos] = value;

}
  System.out.println("---------");
  myArrayList.set(0,100);
  myArrayList.disPlay();

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

public void remove(int key) {

    if(isEmpty()){
        throw new EmptyException("顺序表为空");
    }
    int index = this.indexOf(key);
    if(index==-1){
        System.out.println("没有这个数字");
    }
    for (int i = index; i < size()-1; i++) {
            this.elem[i] = this.elem[i+1];
    }
    this.usedSize--;
}
    System.out.println("---------");
    myArrayList.remove(3);
    myArrayList.disPlay();

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

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



相关文章
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
1296 4
|
存储 XML jenkins
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
3670 1
iOS 底层原理38:自动化打包(一)Xcode + Shell脚本
|
JSON Dart API
Flutter dio http 封装指南说明
本文介绍了如何实现一个通用、可重构的 Dio 基础类,包括单例访问、日志记录、常见操作封装以及请求、输出、报错拦截等功能。
468 2
Flutter dio http 封装指南说明
|
XML JSON 前端开发
一文带你了解 Flutter dio封装
一文带你了解 Flutter dio封装
1803 1
|
负载均衡 API 数据格式
RPC和HTTP的区别?
RPC和HTTP的区别?
1099 0
Flutter-实现扫描线移动效果
本文详细介绍了如何使用Flutter创建一个可重用的ScanLineMovingBar控件,通过AnimationController和AnimatedBuilder实现扫描线从上到下的移动效果,适用于二维码/条形码扫描场景。
330 0
|
iOS开发 容器
Flutter 进度条
Flutter 进度条
237 0
|
API 容器
Flutter 自定义实现时间轴、侧边进度条
Flutter 自定义实现时间轴、侧边进度条
542 0
|
前端开发 API Android开发
Flutter最强大的图表库fl_chart的使用
Flutter最强大的图表库fl_chart的使用
1636 1
|
Dubbo 网络协议 Java
RPC框架:一文带你搞懂RPC
这篇文章全面介绍了RPC(远程过程调用)的概念、原理和应用场景,解释了RPC如何工作以及为什么在分布式系统中广泛使用,并探讨了几种常用的RPC框架如Thrift、gRPC、Dubbo和Spring Cloud,同时详细阐述了RPC调用流程和实现透明化远程服务调用的关键技术,包括动态代理和消息的编码解码过程。
RPC框架:一文带你搞懂RPC