【浅学Java】简单模拟实现 ArrayList

【浅学Java】简单模拟实现 ArrayList,第1张

想要用好Java中的提供的ArrayList,你就得十分熟悉ArrayList的原理,通过模拟实现ArrayList,就可以为以后阅读ArrayList源码打下基础,从而为熟悉ArrayList底层原理打下基础。

下面我们先来简单分析一下以下接口的实现细节:

public class MyArrayList {
    public int []elem;//设置数组
    public int usedSize;//设置下标
    private static final int DEFAULT_SIZE=10;//设置数组的默认大小

    //设置一个构造方法
    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
        this.usedSize = 0;
    }
    // 打印顺序表
    public void display() {
        直接遍历即可
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
         1.判断数组是否满了
         2.满了就进行扩容,不满就进行插入 *** 作
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        1.判断pos的合理性,不合理就扔出异常(程序停下来)
        2.判断数组是否满了,满了就进行扩容
        3.进行插入 *** 作
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        遍历数组,找到返回true,没有找到就返回false
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        遍历数组,找到就返回数组下标,没有找到就返回-1
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        1.判断数组是否为空,为空就扔出"数组为空"的异常
        2.判断pos的合理性,不合理就扔出异常;
        3.前两步都没有问题时就返回pos位置的值
    }
    // 给 pos 位置的元素设为 value————注意,这里不会给顺序表添加元素
    public void set(int pos, int value) {
        1.检查数组是否为空,为空就扔出"数组为空"的异常
        2.判断pos的合理性,不合理就扔出异常;
        3.前两步都没有问题时,就将pos 位置的元素设为 value
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        1.检查数组是否为空,为空就扔出"数组为空"的异常
        2.第一步没问题,遍历数组找到值为toRemove的下标,然后就进行删除 *** 作
    }
    // 获取顺序表长度
    public int size() {
        直接return usedSize
    }
    // 清空顺序表
    public void clear() {
        直接将usedSize置为0
    }
}

大家可以参照上面的思路实现一遍,然后再来看下面的代码:

public class MyArrayList {
    public int []elem;
    public int usedSize;
    private static final int DEFAULT_SIZE=10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
        this.usedSize = 0;
    }
    //判断数组是否满了
    public boolean isFull(){
        return this.elem.length==this.usedSize;
    }
    private boolean checkPos(int pos){
        if(pos<0||pos>this.usedSize){
            System.out.println("pos位置不合法");
            return false;
        }
        return true;
    }
    private boolean isEmpty(){
        return this.usedSize==0;
    }


    //打印顺序表
    public void display() {
        for(int i=0;i<this.usedSize;i++){
            System.out.printf("%d ",this.elem[i]);
        }
        System.out.println();
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
        //判断是否满了,如果满了就进行扩容
        if(isFull()){
            this.elem=Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //不满就进行插入
        this.elem[this.usedSize]=data;
        this.usedSize++;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(!checkPos(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]=elem[i];
        }
        this.elem[pos]=data;
        this.usedSize++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for(int i=0;i<this.usedSize;i++){
            if(toFind==this.elem[i]){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for(int i=0;i<this.usedSize;i++){
            if(toFind==this.elem[i]){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        if(!checkPos(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合理");
        }
        if(isEmpty()){
            throw new MyArrayListEmptyException("获取元素时,顺序表为空");
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if(!checkPos(pos)){
            throw new MyArrayListIndexOutOfException("pos位置不合理");
        }
        if(isEmpty()){
            throw new MyArrayListIndexOutOfException("顺序表为空");
        }
        this.elem[pos]=value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if(isEmpty()){
            throw new MyArrayListIndexOutOfException("顺序表为空");
        }
        for(int i=0;i<this.usedSize;i++){
            if(toRemove==this.elem[i]){
                for(int j=i;j<this.usedSize-1;j++){
                    this.elem[j]=this.elem[j+1];
                }
                this.usedSize--;
                return;
            }
        }
        System.out.println("不存在你要删除的数据");
    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize=0;
        System.out.println("顺序表已清空");
    }
}

下面来进行一些测试 *** 作:

public class TestDemo {
    public static void main(String[] args) {
        MyArrayList myArrayList=new MyArrayList();//实例化MyArrayList对象啊
        for(int i=0;i<10;i++){
            myArrayList.add(i);//先在顺序表中插入一些数据
        }
        myArrayList.display();//打印顺序表
        System.out.println("===================");
        boolean ret1= myArrayList.contains(9);//查看9是否在顺序表中
        System.out.println(ret1);
        System.out.println("===================");
        int ret2=myArrayList.indexOf(6);//查看6在顺序表中的下标位置
        System.out.println(ret2);
        System.out.println("===================");
        int ret3=myArrayList.get(5);//获取下标为5位置处的值
        System.out.println(ret3);
        System.out.println("===================");
        myArrayList.set(0,999);//把0下标的值置为999
        myArrayList.display();
        System.out.println("===================");
        myArrayList.remove(4);//删除第一次出现的4
        myArrayList.display();
        System.out.println("===================");
        int len=myArrayList.usedSize;//获取顺序表长度
        System.out.println(len);
        System.out.println("===================");
        myArrayList.clear();//清空顺序表
        myArrayList.display();
        System.out.println("===================");
    }
}

上面基本模拟了ArrayList的基本 *** 作,想要进一步学习ArrayList,请看下一篇文章~~

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/737540.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存