模拟实现ArrayList类

本篇学习如何模拟实现ArrayList类

结构

1、异常

定义两个异常,顺序表为空异常及数组下表不合法

顺序表为空异常

public class EmptyException extends RuntimeException{
    public EmptyException() {
        super();
    }

    public EmptyException(String message) {
        super(message);
    }
}

数组下表不合法

public class PosIllegal extends RuntimeException{
    public PosIllegal(){

    }

    public PosIllegal(String message) {
        super(message);
    }
}

2、MyArrayList类

共包含增删改查等方法。代码有注释

public class MyArrayList {
    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     *   根据usedSize判断即可
     */
    public void display() {
        for (int i = 0; i <usedSize ; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println("");
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(isFull()){
            grow();
        }
     this.elem[this.usedSize++]=data;
    }

    /**
     * 判断当前的顺序表是不是满的!
     * @return true:满   false代表空
     */
    public boolean isFull() {
        return this.usedSize==elem.length;
    }

    //检查pos位置是否合法,用于add方法
    private boolean checkPosInAdd(int pos) throws PosIllegal{
        if(pos<0||pos>usedSize) {
            throw new PosIllegal("pos位置不合法");
            //合法
        }
        return false;
    }
    //检查pos位置是否合法,用于删,改,查
    private boolean checkPos(int pos) throws PosIllegal{
        if(pos<0||pos>=usedSize) {
            throw new PosIllegal("pos位置不合法");
            //合法
        }
        return false;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
      try {
          checkPosInAdd(pos);
          if(isFull())
              grow();

          for (int i =usedSize-1 ; i>=pos ; i--) {
              elem[i+1]=elem[i];
          }
          elem[pos]=data;
         usedSize++;
      }catch(PosIllegal e){
            e.printStackTrace();
          System.out.println("插入元素pos位置不合法");
        }
      }

      //扩容
    private void grow(){
        this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
  }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i]==toFind)
                return true;
        }
       return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(elem[i]==toFind)
                return i;
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos(pos);
            return elem[pos];
        }catch(PosIllegal e){
            e.printStackTrace();
            System.out.println("pos位置不合法");
        }catch(EmptyException e){
            e.printStackTrace();
            System.out.println("pos位置为空");
        }
        return -1;
    }

    //判断是否为空,抛出异常
    private void checkEmpty(){
        if(isEmpty())
            throw new EmptyException("顺序表为空");
    }
    //判断是否为空
    private boolean isEmpty() {
        return usedSize==0;
    }
    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos(pos);
            elem[pos]=value;
        }catch(PosIllegal e){
            e.printStackTrace();
            System.out.println("pos位置不合法");
        }catch(EmptyException e){
            e.printStackTrace();
            System.out.println("pos位置为空");
        }
    }

    /**
     * 删除第一次出现的关键字key
     * @param key
     */
    public void remove(int key) {
       try{
           checkEmpty();
           int pos = indexOf(key);
           for (int i = pos; i < usedSize-1; i++) {
               elem[i]=elem[i+1];
           }
           usedSize--;
       }catch (EmptyException e){
           e.printStackTrace();
       }
    }

    // 获取顺序表长度
    public int size() {
        return usedSize;
    }

    // 清空顺序表
    public void clear() {
          usedSize=0;
    }

}

3、测试类

public class Test {
    public static void main(String[] args) {
        MyArrayList myArrayList=new MyArrayList();
        myArrayList.add(5);
        myArrayList.add(1);
        myArrayList.add(3);
        myArrayList.add(7);
        myArrayList.add(0);
       
        //打印数字为4的下标
        int ret= myArrayList.indexOf(4);
        System.out.println(ret);

         //移除数字3
        myArrayList.remove(3);
        //设置数组下标4的位置为1
        myArrayList.set(4,1);
        //打印数组
        myArrayList.display();

    }
}

相关推荐

  1. 数据结构 模拟实现ArrayList顺序表

    2024-07-13 22:00:03       49 阅读
  2. string模拟实现

    2024-07-13 22:00:03       24 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 22:00:03       66 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 22:00:03       70 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 22:00:03       57 阅读
  4. Python语言-面向对象

    2024-07-13 22:00:03       68 阅读

热门阅读

  1. C语言程序设计核心详解 第三章:顺序结构

    2024-07-13 22:00:03       19 阅读
  2. Windows系统网络配置命令详细指南

    2024-07-13 22:00:03       17 阅读
  3. PHP语言教程与实战案例

    2024-07-13 22:00:03       23 阅读
  4. 在线课程平台

    2024-07-13 22:00:03       24 阅读
  5. @Autowired 和 @Resource 的区别

    2024-07-13 22:00:03       16 阅读
  6. 基于深度学习的语言生成

    2024-07-13 22:00:03       22 阅读
  7. 华为OD机考题(HJ6 质数因子)

    2024-07-13 22:00:03       21 阅读