实现ArrayList类(经典面试题)

目录

       ArrayList类的实现  

ArrayList的增加

ArrayList的删除

   


       ArrayList类的实现 

      在Java数据结构中,ArrayList是通过数组实现,通过索引(数组下标)来访问元素,支持快速随机访问。

import java.util.Arrays;
public class MyArrayList {
    private int [] elem;
    //原来存放数据元素
    private int usedSize;
    // 代表当前顺序表当中的有效数据个数
    public MyArrayList() {
        this.elem = new int[1000];
    }
    /*
    * 指定容量,实际上是变量数组
    */
    public MyArrayList(int initCapacity) {
        this.elem = new int [initCapacity];
    }
}

判断是否ArrayList为满

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

在数组结尾增加元素 

   //新增元素,在数组最后加
    public void add(int data) {
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        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;
    }
    //获取pos 位置的元素
    public int get(int pos) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("位置不合法");
        }
        return this.elem[pos];
    }

获取顺序表长度,清空顺序表

   //获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    //清空顺序表
    public void clear() {
        this.usedSize = 0;
    }

实现ArrayList最难的点在于增加和删除元素

ArrayList的增加

6101cdfbc99844b6b0d86e579ad975ca.png

 //在pos位置新增元素
    public void add(int pos,int data) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("位置不合法");
        }
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //数组中间加入新的元素,要保持原有数据不被替换,须在把数组 pos位置给空出来
        //采用将pos(包括pos)后面的元素,都往后移动一个单位
        for(int i = this.usedSize-1;i >= pos;i++ ) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

ArrayList的删除

删除的逻辑和增加类似

    //删除第一次出现的关键字
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有这个数据!");
        }
        //这里的逻辑和 在pos位置增加一个元素类似
        // 当数组少一个元素时,后面的元素须得都往前一个位置,把这个被删除的位置给补上
        for(int i = index;i <= this.usedSize;i++) {
            this.elem[i] = this.elem[i+1];
        }
        //this.elem[usedSize-1] = null;
        this.usedSize--;
    }

       全部代码展示

import java.util.Arrays;
public class MyArrayList {
    private int [] elem;
    //原来存放数据元素
    private int usedSize;
    // 代表当前顺序表当中的有效数据个数
    public MyArrayList() {
        this.elem = new int[1000];
    }
    /*
    * 指定容量,实际上是变量数组
    */
    public MyArrayList(int initCapacity) {
        this.elem = new int [initCapacity];
    }
    public void display() {
        for(int i = 0;i < this.usedSize;i++) {
            System.out.print(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;
        usedSize++;
    }
    //判断是否为满
    public boolean isFull() {
        if(this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }
    //在pos位置新增元素
    public void add(int pos,int data) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("位置不合法");
        }
        if(isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //数组中间加入新的元素,要保持原有数据不被替换,须在把数组 pos位置给空出来
        //采用将pos(包括pos)后面的元素,都往后移动一个单位
        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;
    }
    //获取pos 位置的元素
    public int get(int pos) {
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("位置不合法");
        }
        return this.elem[pos];
    }
    //删除第一次出现的关键字
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有这个数据!");
        }
        //这里的逻辑和 在pos位置增加一个元素类似
        // 当数组少一个元素时,后面的元素须得都往前一个位置,把这个被删除的位置给补上
        for(int i = index;i <= this.usedSize;i++) {
            this.elem[i] = this.elem[i+1];
        }
        //this.elem[usedSize-1] = null;
        this.usedSize--;
    }
    //获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    //清空顺序表
    public void clear() {
        this.usedSize = 0;
    }

}

 

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值