java模拟实现ArrayList

这个博客展示了如何从头实现一个简单的顺序表,包括构造方法、动态扩容、添加元素、在指定位置插入元素、检查元素是否存在、查找元素位置、获取和设置元素、删除元素以及获取顺序表长度和清空顺序表等基本操作。示例代码详细解释了每个方法的实现逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大体框架

public class MyArrayList {
    public int[] elem;//null
    int usedSize;//默认0

    //构造方法,分配内存
    public MyArrayList(){
        elem = new int[5];
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) { }
    
    // 打印顺序表
    public void myToString() { }
    
    // 在 pos 位置新增元素
    public void add(int pos, int data) { }
    
    // 判定是否包含某个元素
    public boolean contains(int toFind) { return true; }
    
    // 查找某个元素对应的位置
    public int indexOf(int toFind) { return -1; }
    
    // 获取 pos 位置的元素
    public int get(int pos) { return -1; }
    
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) { }
    
    //删除第一次出现的关键字key
    public void remove(int toRemove) { }
    
    // 获取顺序表长度
    public int size() { return 0; }
    
    // 清空顺序表
    public void clear() { }
}

模拟实现

public class MyArrayList {
    public int[] elem;//null
    int usedSize;//默认0

    public MyArrayList(){//构造方法,分配内存
        elem = new int[5];
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        //判满
        if(isFull()){
            //扩容为原来二倍
            this.elem = Arrays.copyOf(elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        usedSize++;
    }

    //判断数组是否为满
    public boolean isFull(){
        if(this.usedSize == this.elem.length)
            return true;
        return false;
    }

    // 打印顺序表
    public void myToString() {
        for(int index = 0; index < this.usedSize; ++index){
            System.out.print(this.elem[index]+" ");
        }
        System.out.println();//换行
    }

    // 在 pos 位置新增元素(方法重载)
    public void add(int pos, int data) {
        //判满扩容
        if(this.isFull()){
            this.elem = Arrays.copyOf(elem,2*this.elem.length);
        }
        //pos是否合法
        if(pos < 0 || pos > this.usedSize){
            System.out.println("插入下标不合法!");
            return ;
        }
        //后退
        for(int index = this.usedSize - 1; index >= pos; index--){
            this.elem[index + 1] = this.elem[index];
        }
        //插入
        this.elem[pos] = data;
        //长度加1
        this.usedSize++;
    }

    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for(int index = 0; index < this.usedSize; index++){
            if(this.elem[index] == toFind){
                return true;
            }
        }
        return false;
    }

    // 查找某个元素首次出现对应的位置
    public int indexOf(int toFind) {
        for(int index = 0; index < this.usedSize; index++){
            if(this.elem[index] == toFind){
                return index;
            }
        }
        return -1;//数组下标没有-1
    }

    // 获取 pos 位置的元素
    public int get(int pos) {
        //pos是否合法
        if(pos < 0 || pos > usedSize - 1){
            throw new RuntimeException("下标位置不合法,无法获取该元素");
        }
        //获取元素
        return this.elem[pos];
    }

    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        //pos是否合法
        if(pos < 0 || pos > usedSize){
            return ;
        }
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        //判空
        if(isEmpty()){
            System.out.println("当前为空,无法删除!");
            return;
        }

        //寻找下标,也可以使用int index = indexOf(toRemove)
        int pos = 0;
        for(int index = 0; index < this.usedSize; index++){
            if(this.elem[index] == toRemove){
                pos = index;
                break;
            }
        }
        //覆盖
        for(int index = pos; index < this.usedSize - 1; index++){
            this.elem[index] = this.elem[index+1];
        }
        this.usedSize--;
    }
    //判空
    public boolean isEmpty(){
        if(this.usedSize == 0){
            return true;
        }
        return false;
    }
    
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值