Implement an ArrayList in java

By looking at the underlying code of ArrayList, it is found that ArrayList has the following properties:

SearchList:

1. If the capacity of the ArrayList is not specified when it is initialized, the default size is 10, if the capacity is not enough, expand the capacity according to the "newCapacity=oldCapaCity+(oldCapaCity >> 1)" algorithm (1.5 times the original capacity for expansion);

2. ArrayList thread is not safe. To achieve thread safety, you can use the synchronized keyword or Collections.synchronizedList() method to initialize, as follows

List list = Collections.synchronizedList(new ArrayList<>()); 或者使用:

3. The underlying data structure of ArrayList is Object; the type of array (Object[] elementData), so many of its methods are carried out in accordance with the processing method of arrays.

4. Variables modified by the transient keyword indicate that they are not serialized.

Among them, the System.arraycopy() method in the JDK will be used. This method can copy an array src into another array dest. It is a Native method, so we only pay attention to the meaning of the incoming parameters. The source code is as follows:

    public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

The meaning of each parameter:

1. Object src: The original array is the array we need to copy

2. int srcPos: which means starting from the subscript of the original array

3. Object dest: Indicates the array after copying (it can be understood as a new array, but not a new array)

4. int destPos: indicates that the data after copying is stored from the subscript of the new array

5. int length: indicates the length copied from the original array.

Examples are as follows:

public static void main(String[] args) {
        Object[] datas = new Object[20];
        datas[0] = "000";
        datas[1] = "111";
        datas[2] = "222";
        datas[3] = "333";
        datas[4] = "444";
        datas[5] = "555";
        datas[6] = "666";
        datas[7] = "777";
        datas[8] = "888";
        datas[9] = "999";
        // 从datas中的第2个数据开始复制,复制长度为8,然后从第三个下标开始存放在新数组datas中
        System.arraycopy(datas,2,datas,3,8);
        for (int i = 0; i < datas.length; i++) {
            System.out.print(datas[i]+" ");
        }
    }

Results of the:

000 111 222 222 333 444 555 666 777 888 999 null null null null null null null null null 

Through the above properties, we can manually implement a simple ArrayList by ourselves, the code is as follows:

public class MyArrayList {
    private Object[] elementData;
    private int size;
    private int defaultCapacity = 10;

    public int size() {
        return size;
    }

    public MyArrayList(){
        this(10);
    }

    public MyArrayList(int initCapacity) {
        if(initCapacity == 0){
            this.elementData = new Object[defaultCapacity];
        }else if (initCapacity > 0){
            this.elementData = new Object[initCapacity];
        }else{
            throw new IllegalArgumentException("初始化容量参数有误");
        }
    }
    // 获取某个节点的数据
    public Object get(int index){
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException();
        }
        return elementData[index];
    }

    // 往数组中添加元素
    public void add(Object object) {
        ensureCapacity(size +1);
        elementData[size] = object;
        size++;
    }

    // 数组扩容
    public void ensureCapacity(int size){
        int currentCapacity = elementData.length;
        if (size > currentCapacity) {
            int newCapacity = currentCapacity << 1 + currentCapacity;
            Object[] newElementData = new Object[newCapacity];
            System.arraycopy(elementData,0,newElementData,0,currentCapacity);
            elementData = newElementData;
        }
    }
    // 往指定位置添加元素
    public void add(int index, Object data){
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException();
        }
        ensureCapacity(size+1); // 扩容
        System.arraycopy(elementData,index,elementData,index+1,size-index);
        elementData[index] = data;
        size++;
    }

    // 修改指定位置的数据
    public Object set(int index,Object data){
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException();
        }
        Object oldData = elementData[index];
        elementData[index] = data;
        return oldData;
    }

    // 删除指定位置上的元素
    public Object delete(int index){
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException();
        }
        Object oldData = elementData[index];
        if(index == size) {
            elementData[index] = null;
        }else{
            int copyLength = size-index-1;
            System.arraycopy(elementData,index+1,elementData,index,copyLength);
        }
        size --;
        return oldData;
    }

}

Test category:

class MyTest{
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        for (int i = 0; i < 20; i++) {
            myArrayList.add(i+"aaaa");
        }
        MyTest.showData(myArrayList);
        System.out.println("指定位置添加数据--------------------------");
        myArrayList.add(3,"sdssd");
        MyTest.showData(myArrayList);
        System.out.println("修改指定位置的数据--------------------------");
        System.out.println(myArrayList.set(5,"测试"));
        MyTest.showData(myArrayList);
        System.out.println("删除指定位置的数据--------------------------");
        System.out.println(myArrayList.delete(2));
        MyTest.showData(myArrayList);

    }

    private static void showData(MyArrayList myArrayList){
        for (int i = 0; i < myArrayList.size(); i++) {
            System.out.print(myArrayList.get(i) +" ");
        }
        System.out.println("");
    }

}

Results of the:

0aaaa 1aaaa 2aaaa 3aaaa 4aaaa 5aaaa 6aaaa 7aaaa 8aaaa 9aaaa 10aaaa 11aaaa 12aaaa 13aaaa 14aaaa 15aaaa 16aaaa 17aaaa 18aaaa 19aaaa 
指定位置添加数据--------------------------
0aaaa 1aaaa 2aaaa sdssd 3aaaa 4aaaa 5aaaa 6aaaa 7aaaa 8aaaa 9aaaa 10aaaa 11aaaa 12aaaa 13aaaa 14aaaa 15aaaa 16aaaa 17aaaa 18aaaa 19aaaa 
修改指定位置的数据--------------------------
4aaaa
0aaaa 1aaaa 2aaaa sdssd 3aaaa 测试 5aaaa 6aaaa 7aaaa 8aaaa 9aaaa 10aaaa 11aaaa 12aaaa 13aaaa 14aaaa 15aaaa 16aaaa 17aaaa 18aaaa 19aaaa 
删除指定位置的数据--------------------------
2aaaa
0aaaa 1aaaa sdssd 3aaaa 测试 5aaaa 6aaaa 7aaaa 8aaaa 9aaaa 10aaaa 11aaaa 12aaaa 13aaaa 14aaaa 15aaaa 16aaaa 17aaaa 18aaaa 19aaaa 

 

Guess you like

Origin blog.csdn.net/u013804636/article/details/107791815