JAVA自己实现ArrayList

package 集合.list.ArrayList;

import java.util.Arrays;
import java.util.Date;

public class MyArryList {

    //定义一个空的数组
    private final static Object[] myArray = {};
    //定义一个默认长度10
    private final static int default_length = 10;
    Object[] elementData;
    //myArryList长度
    private int size = 0;

    //无参数构造方法,默认容量10
    public MyArryList() {
        this.elementData = myArray;
    }

    //带参数构造函数
    public MyArryList(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("参数不能小于0");
        }
        this.elementData = new Object[length];
        size = length;
    }

    //获取长度
    public int getSize() {
        return this.size;
    }

    //添加元素
    public void add(Object args) {
        //每次添加元素时需要考虑长度问题
        //判断是否需要扩容
        if (size >= elementData.length) {
            this.grown();
        }
        elementData[size++] = args;
    }

    //扩容方法
    private void grown() {
        if (elementData.length <= 1) {
            elementData = Arrays.copyOf(elementData, elementData.length + 1);
        } else {
            elementData = Arrays.copyOf(elementData, elementData.length + (elementData.length >> 1));
        }
    }

    //删除元素
    public void remove(Object obj) {
        //先查找第一次出现的索引
        int i = indexOf(obj);
        if (i != -1) {
            remove(i);
        }
    }

    //删除指定索引位置元素
    public void remove(int index) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("参数越界");
        }
        //遍历数组
        //for(int i = index;i<elementData.length;i++){
        //    elementData[i]=elementData[i+1];
        //}
        System.arraycopy(elementData, index + 1, elementData, index, size - index - 1);
        size--;
    }

    //指定索引插入元素
    public void insert(int index, Object obj) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException("参数越界");
        }
        //
        if (size >= elementData.length) {
            this.grown();
        }
        //[1,2,3,4][1,2,5,3,4]第一种方式
//        System.arraycopy(elementData,index, elementData,index-1,size-index);

        //遍历数组
        for (int i = size - 1; i >= index; i--) {
            elementData[i + 1] = elementData[i];
        }
        //将需要插入元素放入指定索引位置
        elementData[index] = obj;
        size++;
    }

    //查找元素第一次索引
    public int indexOf(Object obj) {
        for (int i = 0; i < size; i++) {
            if (elementData[i] == obj || elementData[i] != null && elementData[i].equals(obj)) {
                return i;
            }
        }
        return -1;
    }

    //更改元素
    public void set(int index, Object obj) {
        elementData[index] = obj;
    }

    //清空数据
    public void clear() {
        elementData = new Object[0];
        size = 0;
    }

    //判断索引是否越界
    public boolean out(int index) {
        if (index < 0 || index > size) {
            return false;
        } else return true;
    }

    //判断是否包含
    public boolean contains(Object obj) {
        return indexOf(obj) != -1;
    }

    //判断是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    //截取子列表
    public MyArryList sublist(int fromIndex, int toIdex) {
        if (fromIndex < 0 || toIdex > size) {
            throw new IndexOutOfBoundsException("越界");
        }
        if (fromIndex > toIdex) {
            throw new IllegalArgumentException();
        }
        MyArryList sublist = new MyArryList(toIdex - fromIndex);
        System.arraycopy(elementData, fromIndex, sublist, 0, toIdex - fromIndex);
        sublist.size = toIdex - fromIndex;
        return sublist;
    }

    //toString重写

    @Override
    public String toString() {
        Object[] newstring =new Object[size];
        System.arraycopy(elementData, 0, newstring, 0, size);

        return  Arrays.toString(newstring);
    }
}

猜你喜欢

转载自blog.51cto.com/12013190/2430394