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

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

顺序表

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

 创建一个类:

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



​

相关文章
|
API 数据安全/隐私保护
Argo CD接入LDAP认证或者gitea认证的方法
argocd默认是通过修改argocd-cm来添加账户的,添加完账户后,还需要使用argocd客户端命令去给账户设置密码,这肯定是比较麻烦的,为了方便使用,我们可以接入ldap认证或者gitea的oauth2认证。 这里我们主要写ldap认证,因为gitea没有提供组信息给dex,而ldap能返回组信息 ,gitea的接入会在文章的末尾进行简单介绍 关键词:argocd ldap dex
2898 1
Argo CD接入LDAP认证或者gitea认证的方法
|
8月前
|
人工智能 自然语言处理 搜索推荐
10分钟上线企业级网站的技术实现与行业思考
阿里云智能建站以AI重构传统建站流程:NLP理解需求、生成式AI自动设计与填充内容、模块化组件+响应式+SEO一体化,10分钟上线专业官网。助力中小企业降本提效,推动开发范式从“手工作坊”迈向工业化。(239字)
|
消息中间件 Oracle Cloud Native
Spring Cloud Alibaba 2022.0.0.0 版本发布啦!
微服务引擎 MSE 面向业界主流开源微服务项目, 提供注册配置中心和分布式协调(原生支持 Nacos/ZooKeeper/Eureka )、云原生网关(原生支持Higress/Nginx/Envoy,遵循Ingress标准)、微服务治理(原生支持 Spring Cloud/Dubbo/Sentinel,遵循 OpenSergo 服务治理规范)能力。
Spring Cloud Alibaba 2022.0.0.0 版本发布啦!
|
物联网 5G
智慧医院在临床治疗中的应用
智慧医院在临床治疗中的应用
262 0
|
数据中心 Perl 安全
|
9天前
|
域名解析 人工智能 Linux
|
19天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17722 63
|
17天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置

热门文章

最新文章