手动实现ArrayList

创建一个MyArrayList,实现ArrayList中的几个方法    


代码片段如下:

package com.dalingjia.collection.list;

/**
 * ArrayList的简单实现(手写)
 *
 */
public class MyArrayList {

    private Object[] elementData;       //底层数组
    private int size;               //数组中元素大小

    public int size(){
        return size;
    }

    public MyArrayList() {
        this(10);
    }

    public MyArrayList(int initialCapacity){
        if(initialCapacity < 0){
            throw new IndexOutOfBoundsException();
        }
        elementData = new Object[initialCapacity];
    }

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

    public Object get(int index){
        rangeCheck(index);
        return elementData[index];
    }

    public boolean add(Object obj){
        //判断长度是否足够
        ensureCapacity();
        elementData[size++] = obj;
        return true;
    }

    /**
     * 添加的元素index只能<=size
     * @param index
     * @param obj
     */
    public void add(int index,Object obj){
        rangeCheckForAdd(index);
        ensureCapacity();
        System.arraycopy(elementData, index, elementData, index + 1, size - index);
        elementData[index] = obj;
        size++;

    }

    public Object remove(int index){

        rangeCheck(index);
        Object oldValue = elementData[index];
        int length = elementData.length - index -1 ;
        if(length >0){
            System.arraycopy(elementData, index+1, elementData, index, elementData.length-index-1);
        }
        elementData[size--] = null;
        return oldValue;
    }

    public boolean remove(Object obj){
        if(obj == 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(obj.equals(elementData[i])){
                    remove(i);
                    return true;
                }
            }
        }
        return false;
    }


    //返回旧值
    public Object set(int index,Object obj){
        rangeCheck(index);
        Object oldValue = elementData[index];
        elementData[index] = obj;
        return oldValue;
    }

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

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

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

    public void ensureCapacity(){

        //开始扩容
        if(size == elementData.length){
            Object[] newArray = new Object[size * 2 + 1];
            System.arraycopy(elementData,0, newArray, 0, elementData.length);
            elementData = newArray;
        }
    }

    public String showList(){
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < size; i++) {
            stringBuffer.append(elementData[i]);
            if(i != size-1){
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        System.out.println(myArrayList.elementData.length);//底层数组长度
        myArrayList.add("tanhq");
        myArrayList.add("jingling");
        myArrayList.add("longge");
        myArrayList.add("longge");
        myArrayList.add(1, "gege");
        System.out.println(myArrayList.isEmpty());
        System.out.println(myArrayList.get(2));
        myArrayList.remove(2);
        myArrayList.remove("gege");
        myArrayList.set(2, "jj");
        System.out.println(myArrayList.showList());
    }

}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值