Java模拟实现ArrayList顺序表

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() {

        //usedSize==0

        for (int i = 0; i < this.usedSize; i++) {

            System.out.print(this.elem[i]+" ");

        }

        System.out.println();

    }

 

    // 新增元素,默认在数组最后新增

    public void add(int data) {

        //1、判断是否是满的,如果满的,那么进行扩容

        if(isFull()) {

            //扩容

            this.elem = Arrays.copyOf(this.elem,2 * this.elem.length);

        }

        //2、不满进行插入

        this.elem[this.usedSize++] = data;
    }

 

    /**

     * 判断当前的顺序表是不是满的!

     * @return true:满   false代表空

     */

    public boolean isFull() {

        return this.usedSize == this.elem.length;

    }

 

 

    private boolean checkPosInAdd(int pos) {

        if(pos < 0 || pos > this.usedSize) {

            System.out.println("pos位置不合法");

            return false;

        }

        return true;//合法

    }

 

    // 在 pos 位置新增元素

    public void add(int pos, int data) {

        //判断下标是否是合法的

        if(!checkPosInAdd(pos)) {

            throw new MyArrayListIndexOutOfException("添加方法的pos不合理!");

        }

        //判断是否是满的

        if (isFull()) {

            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);

        }

        //挪数据

        for (int i = this.usedSize-1; i >= pos ; i--) {

            this.elem[i+1] = this.elem[i];

        }

        //挪完了数据

        this.elem[pos] = data;

        this.usedSize++;

    }

 

    // 判定是否包含某个元素

    public boolean contains(int toFind) {

        for (int i = 0; i < this.usedSize; i++) {

            if(this.elem[i] == toFind) {

                return true;

            }

        }

        return false;

    }

    // 查找某个元素对应的位置

    public int indexOf(int toFind) {

        for (int i = 0; i < this.usedSize; i++) {

            if(this.elem[i] == toFind) {

                return i;

            }

        }

        return -1;

    }

 

 

    private boolean checkPosInGet(int pos) {

        if(pos < 0 || pos >= this.usedSize) {

            System.out.println("pos位置不合法");

            return false;

        }

        return true;//合法

    }

 

    // 获取 pos 位置的元素

    public int get(int pos) {

        if(!checkPosInGet(pos)) {

            throw new MyArrayListIndexOutOfException("获取pos下标时,位置不合法");

        }

        //不用写判断空不空 没有问题的

        if(isEmpty()) {

            throw new MyArrayListEmptyException("获取元素的时候,顺序表为空!");

        }

        return this.elem[pos];

    }

 

 

    private boolean isEmpty() {

        return this.usedSize == 0;

    }

 

    // 给 pos 位置的元素设为【更新为】 value

    public void set(int pos, int value) {

        if(!checkPosInGet(pos)){

            throw new MyArrayListIndexOutOfException("更新pos下标的元素,位置不合法");

        }

        //如果合法 ,那么其实不用判断顺序表为空的状态了

        if(isEmpty()) {

            throw new MyArrayListEmptyException("顺序表为空!");

        }

        //顺序表为满的情况也可以更新

        this.elem[pos] = value;

    }

 

 

    /**

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

     * @param key

     */

    public void remove(int key) {

        if(isEmpty()) {

            throw new MyArrayListEmptyException("顺序表为空,不能删除!");

        }

        int index = indexOf(key);

        if(index == -1) {

            System.out.println("不存在你要删除的数据");

            return;

        }

 

        for (int i = index; i < this.usedSize-1; i++) {

            this.elem[i] = this.elem[i+1];

        }

        //删除完成

        this.usedSize--;

        // this.elem[usedSize] = null; 如果是引用类型 这里需要置为空

    }

 

 

    // 获取顺序表长度

    public int size() {

        return this.usedSize;

    }

 

    // 清空顺序表

    public void clear() {

        /*

        如果是引用数据类型 得一个一个置为空 这样做才是最合适的

        for (int i = 0; i < this.usedSize; i++) {

            this.elem[i] = null;

        }

        this.usedSize = 0;

        */

 

        this.usedSize = 0;

    }

异常类

public class MyArrayListIndexOutOfException extends RuntimeException{
    public MyArrayListIndexOutOfException() {
    }

    public MyArrayListIndexOutOfException(String message) {
        super(message);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小小小关同学

你的支持就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值