手写ArrayList

 写一个自定义ArrayList有利于提升自己对java数据结构的理解。 ArrayList底层使用数组。数组的难点在于在数组中间插入,删除麻烦。优点查询速度快。

 直接上代码  

  一个简单的集合有以下方法,我们使用接口MyList定义方法。

public interface MyList<E> {

	public boolean add(E e);

	public boolean add(int index, E e);

	public E remove(int index);

	public boolean remove(E e);

	public int size();

	public E get(int index);

}

  

有了方法,我们只需要关注业务逻辑,具体代码实现。

import java.util.Arrays;

public class MyArrayList<E> implements MyList<E> {

    private Object[] elementData;

    private int size;

    private static final Object[] EMPTY_ELEMENTDATA = {};

    public MyArrayList() {
        this(10);
    }

    public MyArrayList(int initialCapacity) {
        if (initialCapacity > 0) {
            this.elementData = new Object[initialCapacity];
        } else if (initialCapacity == 0) {
            this.elementData = EMPTY_ELEMENTDATA;
        } else {
            throw new IllegalArgumentException("Illegal Capacity: " + initialCapacity);
        }
    }

    public int size() {
        return size;
    }

    /**
     * 数组扩容
     * 
     * @param minCapacity
     */
    private void ensureCapacityInterval(int minCapacity) {
        int oldCapacity = elementData.length;
        // 扩容原来的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        // 防止出现原数组 长度为0的情况
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        // 扩容
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    /**
     * 插入 第一步 检查是否需要扩容 第二步 赋值
     */
    public boolean add(E e) {
        ensureCapacityInterval(size + 1);
        elementData[size++] = e;
        return true;
    }

    /**
     * 指定位置 add
     * 
     * @param index
     * @param obj
     */
    public boolean add(int index, Object obj) {
        rangeCheck(index);
        ensureCapacityInterval(size + 1);
        // 插入操作
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = obj;
        size++;
        return true;
    }

    public E remove(int index) {
        rangeCheck(index);
        E oldValue = elementData(index);
        // 移除操作
        System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
        elementData[--size] = null;
        return oldValue;
    }

    @SuppressWarnings("unchecked")
    E elementData(int index) {
        return (E) elementData[index];
    }

    public boolean remove(Object object) {
        // 数组移除分二种情况 一种为空
        if (object == null) {
            for (int i = 0; i < size; i++) {
                if (elementData[i] == null) {
                    remove(i);
                    return true;
                }
            }

        } else {
            // 另外一种判断对象是否相等
            for (int i = 0; i < size; i++) {
                if (get(i).equals(object)) {
                    remove(i);
                    return true;
                }
            }
        }
        return false;
    }

    public E get(int index) {
        rangeCheck(index);
        return elementData(index);
    }

    private void rangeCheck(int index) {
        if (index > size || index < 0) {
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }
    }

    private String outOfBoundsMsg(int index) {
        return "Index: " + index + ", Size: " + size;
    }

}

 

转载于:https://www.cnblogs.com/laolei11/p/11233139.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值