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

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

顺序表

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

 创建一个类:

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



相关文章
|
25天前
|
算法
数据结构和算法学习记录——复习静态顺序表的两个接口函数(在指定位置插入数据,在指定位置删除数据)
数据结构和算法学习记录——复习静态顺序表的两个接口函数(在指定位置插入数据,在指定位置删除数据)
11 0
|
2月前
|
存储 索引 Python
数组的操作方法
数组的操作方法
68 1
|
11月前
基于数组的一个简单增删改查
基于数组的一个简单增删改查
28 0
|
9月前
|
人工智能 C语言
线性表的定义和基本操作
线性表的定义和基本操作
|
11月前
|
存储
【双链表增删查改接口的实现】
【双链表增删查改接口的实现】
34 0
|
11月前
|
编译器
【单链表增删查改接口的实现】
【单链表增删查改接口的实现】
67 0
|
人工智能
线性表的定义和基本操作(三)
线性表的定义和理解,和一些基本的操作,并且有例题
84 0
|
存储 编译器 C++
【C++要笑着学】vector 常用接口介绍 | 遍历操作 | 空间操作 | 增删查改(一)
本章开始讲解 vector,首先对 vector 进行介绍,然后讲解 vector 常用的接口。像 emplace 等涉及右值引用的接口,我们等后期讲C++11的时候再作讲解。话不多说,直接开讲。
188 1
【C++要笑着学】vector 常用接口介绍 | 遍历操作 | 空间操作 | 增删查改(一)
数据结构103-插入和修改操作封装1
数据结构103-插入和修改操作封装1
65 0
数据结构103-插入和修改操作封装1
数据结构104-插入和修改操作封装2
数据结构104-插入和修改操作封装2
45 0
数据结构104-插入和修改操作封装2