Java实现ArrayList思路及代码

数组ArrayList的实现

说明:

  1. 该类将保存基础数组和数组的容量大小以及当前存储个数
  2. 该类将提供一种机制改变基础数组的容量。
  3. 该类将提供get和set的实现
  4. 该类将提供基本的例程,如获取数组大小size、是否为空isEmpty、清空数组clear、添加add等功能
  5. 该类将提供一个数组接迭代器存储有迭代序列中下一项的下标以及next、hasNext和remove等方法的实现。

下面进行部分方法的实现。完整代码放在结尾

ensureCapacity

public void ensureCapacity(int newCapacity) {
    if (newCapacity < theSize) {
        return;
    }

    AnyType[] old = theItems;
    theItems = (AnyType[]) new Object[newCapacity];
    for (int i = 0; i < size(); i ++) {
        theItems[i] = old[i];
    }
}

该方法用于数组容量的修改。

该方法的实现思路是,先判断需要改变成的容量是否小于当前存储的数据数,如果小于则不进行处理。如果大于则进行如下操作。

将旧数组数据保存下来。创建一个新的数组,将旧数组的数据存入新数组中。

这里要说明一点:泛型数组的创建是非法的。应对的策略是创建一个Object类的数组将其强转为泛型类型。

clear

public void clear() {
    doClear();
}

private void doClear() {
    theSize = 0;//将存储数据初始化
    ensureCapacity(DEFAULT_CAPACITY);//将数组大小初始化
}

即将数组初始化。

add

public boolean add(AnyType x) {
    add(size(), x);
    return true;
}

public void add(int idx, AnyType x) {
    //保证容量足够
    if (theItems.length >= size()) {
        ensureCapacity(size() * 2 + 1);
        add(idx, x);
        return;
    }
    //将数组加入中间时,该位置以后的数据全体后移
    for (int i = theSize; i > idx; i --) {
        theItems[i] = theItems[i - 1];
    }
    //后移完毕后加入数据
    theItems[idx] = x;
    theSize ++;
}

添加数据时,先判断容量是否足够,若不足够则扩容为原来的2倍加1。若容量足够则进行如下操作

从当前位置开始将当前位置之后的数据后移。后移完毕后插入数据。后移的实现方法是:因为后移要获取前一位的数据,所以从最后一位数据的后一位开始获取前一位的数据,当插入位置的后一位获取了插入位置的数据即后移完毕。

remove

public void remove(int index) {
    //将指定位置之后的数据全体前移
    for (int i = index; i < size() - 1; i ++) {
        theItems[i] = theItems[i + 1];
    }

    theSize --;
}

由删除位置开始获取后一位的数据对当前位置进行覆盖,当最后一位的前一位被覆盖则前移完成,即删除完毕。

iterator

public Iterator<AnyType> iterator() {
    return new ArrayListIterator();
}

private class ArrayListIterator implements Iterator<AnyType> {
    private int current = 0;//当前位置的下一位的下标
    @Override
    public boolean hasNext() {
        return current < size();
    }

    @Override
    public AnyType next() {
        if (hasNext()) {
            return (AnyType) new NoSuchElementException();
        }
        return theItems[current ++];
    }

    public void remove(){
        MyArrayList.this.remove(--current);
    }
}

迭代器采用内部类实现。current存储下一位的下标。当current==size()时说明已经没有到达数组末尾。

获取next时,先判断是否存在,若存在则返回theItems[current]再后移。

删除则先将current前移再删除元素

完整代码

/**
 * @Author: 西电秃子
 * @Description: ArrayList类的实现
 * @Date: Created in 19:42 2021/9/22
 * @Version: v1.0
 */
public class MyArrayList<AnyType> implements Iterable<AnyType> {
    private static final int DEFAULT_CAPACITY = 10;//基础容量

    private int theSize;//当前存储数量
    private AnyType[] theItems;//基础数组

    public MyArrayList() {
        doClear();
    }

    public void clear() {
        doClear();
    }

    /**
     * @Author: pocean
     * @Description: 初始化数组
     * @Date: 19:53 2021/9/22
     */
    private void doClear() {
        theSize = 0;//初始化
        ensureCapacity(DEFAULT_CAPACITY);//保证容量足够
    }

    public int size() {
        return theSize;
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    /**
     * @Author: pocean
     * @Description: 调用此函数防止空间浪费
     * @Date: 19:52 2021/9/22
     */
    public void trimToSize() {
        ensureCapacity(size());
    }

    /**
     * @Author: pocean
     * @Description: 获取指定位置的数据
     * @Date: 19:53 2021/9/22
     */
    public AnyType get(int idx) {
        if (idx < 0 || idx >= size()) {
            throw new ArrayIndexOutOfBoundsException();//角标越界
        }
        return theItems[idx];
    }

    /**
     * @Author: pocean
     * @Description: 设定数组指定位置的值,返回原来的值
     * @Date: 19:55 2021/9/22
     */
    public AnyType set(int idx, AnyType newVal) {
        if (idx < 0 || idx >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        AnyType old = theItems[idx];
        theItems[idx] = newVal;
        return old;
    }

    /**
     * @Author: pocean
     * @Description: 设定数组指定位置的值,无返回值
     * @Date: 19:58 2021/9/22
     */
    public void set(AnyType newVal, int idx) {
        if (idx < 0 || idx >= size()) {
            throw new ArrayIndexOutOfBoundsException();
        }
        theItems[idx] = newVal;
    }

    /**
     * @Author: pocean
     * @Description: 保证容量足够
     * @Date: 20:03 2021/9/22
     */
    public void ensureCapacity(int newCapacity) {
        if (newCapacity < theSize) {
            return;
        }

        AnyType[] old = theItems;
        theItems = (AnyType[]) new Object[newCapacity];
        for (int i = 0; i < size(); i ++) {
            theItems[i] = old[i];
        }
    }

    /**
     * @Author: pocean
     * @Description: 添加数据至末尾
     * @Date: 20:04 2021/9/22
     */
    public boolean add(AnyType x) {
        add(size(), x);
        return true;
    }

    /**
     * @Author: pocean
     * @Description: 添加数据至指定位置
     * @Date: 20:05 2021/9/22
     */
    public void add(int idx, AnyType x) {
        //保证容量足够
        if (theItems.length == size()) {
            ensureCapacity(size() * 2 + 1);
            add(idx, x);
            return;
        }
        //将数组加入中间时,该位置以后的数据全体后移
        for (int i = theSize; i > idx; i --) {
            theItems[i] = theItems[i - 1];
        }
        //后移完毕后加入数据
        theItems[idx] = x;
        theSize ++;
    }

    /**
     * @Author: pocean
     * @Description: 删除指定位置数据
     * @Date: 20:10 2021/9/22
     */
    public void remove(int index) {
        //将指定位置之后的数据全体前移
        for (int i = index; i < size() - 1; i ++) {
            theItems[i] = theItems[i + 1];
        }

        theSize --;
    }
    /**
     * @Author: pocean
     * @Description: 返回一个数组迭代器
     * @Date: 20:15 2021/9/22
     */
    @Override
    public Iterator<AnyType> iterator() {
        return new ArrayListIterator();
    }

    /**
     * @Author: pocean
     * @Description: 数组迭代器
     * @Date: 20:15 2021/9/22
     */
    private class ArrayListIterator implements Iterator<AnyType> {
        private int current = 0;//当前位置
        @Override
        public boolean hasNext() {
            return current < size();
        }

        @Override
        public AnyType next() {
            if (hasNext()) {
                return (AnyType) new NoSuchElementException();
            }
            return theItems[current ++];
        }

        public void remove(){
            MyArrayList.this.remove(--current);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值