java implements the basic functions of ArrayList

This article introduces the basic functions of manually implementing an ArrayList.

The bottom layer of ArrayList is a dynamic array, which is different from linked list. The code implements the basic functions of ArrayList, so it does not implement some interfaces implemented by ArrayList, such as RandomAccess and Serializable.

Method name Effect
getSize() Get array size
checkIncrease(int index,Object obj) Expand, double the original array and copy the original data to the new array
indexOf(Object o) Get the index value that first appears in the array according to the element
add(Object o) Add elements to an array
remove(int index) Delete an element in an array. If the element is not at the end of the array, move each element after it one bit forward
checkIndexOut(int index) Check whether the given subscript is out of range
get(int index) Get the specified subscript data
removeAll() Empty array
change(int index,Object o) Modify the value of the corresponding index
contains(Object o) Judge whether the corresponding value is included
isEmpty() Judge whether it is empty

 

public class MyArrayList {

    //Used to store data
    private Object[] data = null;

    //Number of elements in the collection
    private int size = 0;

    //Define a constant of 10. (used later to define the default set size)
    private static final int DEFAULT_CAPACITY = 10;

    /**
     * Parameterless constructor, specifying an initial size of 10
     */
    public MyArrayList(){
        this(DEFAULT_CAPACITY);//Call parametered constructor
    }

    /**
     * Parameter constructor, specifying array size
     * @param initialCapacity
     */
    public MyArrayList(int initialCapacity){
        if(initialCapacity < 0){//Determine the size of the array. Well, it should be negative
            throw new IllegalArgumentException("Illegal set initial capacity value Illegal Capacity: "+initialCapacity);
        }else{
            //Instantiate array
            this.data = new Object[initialCapacity];
        }
    }

    /**
     * Get array size
     * @return
     */
    public int getSize(){
        return size;
    }

    /**
     * Expand, copy and double the original array
     * @param index
     * @param obj
     */
    public void checkIncrease(int index,Object obj){
        if(size >= data.length){//Expand the capacity after the original array is full
            //Instantiate a new array
            Object[] newData = new Object[size*2];

            if(index == -1 && obj == null){
                System.arraycopy(data, 0, newData, 0, size);
            }else{
                //The copy of the object before the index location will be inserted
                System.arraycopy(data, index, newData, index+1, size-index);
            }

            data = newData;
            newData = null;
        }
    }

    /**
     * Get the first index in the collection based on the element
     * @param o
     * @return
     */
    public int indexOf(Object o){
        if(o == null){
            for(int i = 0,len=data.length;i<len;i++){
                if(data[i] == null){
                    return i;
                }
            }
        }else{
            for(int i=0,len=data.length;i<len;i++){
                if(o.equals(data[i])){
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * Add elements at the end
     * @param o Elements to be added
     */
    public boolean add(Object o){
        //Check whether expansion is needed
        checkIncrease(-1,null);
        data[size++] = o;
        return true;
    }

    /**
     * Delete elements according to index
     * @param index
     * @return
     */
    public boolean remove(int index){
        if(checkIndexOut(index)){//Determine whether the array is out of bounds
            if(index == size-1){//If the element to be deleted is the last one
                data[index] = null;
            }else{//If the deleted element is not the last element, you need to move the element after this element forward one bit
                System.arraycopy(data,index+1,data,index,size-index);
            }
            size--;
            return false;
        }else{//Array out of bounds
            throw new IndexOutOfBoundsException("The specified index is out of bounds, and the collection size is:"+size+",The index size you specified is:"+index);
        }
    }

    /**
     * Determine whether the given index is out of range
     * @param index
     * @return
     */
    public boolean checkIndexOut(int index){
        if(index >= size || index <0){
//            throw new IndexOutOfBoundsException("the specified index is out of bounds, the set size is:" + size + ", and the index size you specified is:" + index);
            return false;
        }
        return true;
    }

    /**
     * Get elements from given index
     * @param index
     * @return
     */
    public Object get(int index) {
        checkIndexOut(index);//Judge whether it is out of line
        return data[index];
    }

    /**
     * Delete all elements
     */
    public void removeAll(){
        for(int i=0,len=data.length;i<len;i++){
            data[i] = null;
        }
        size = 0;//
    }

    /**
     * Modify elements according to index
     * @param index
     * @param o
     * @return
     */
    public boolean change(int index,Object o){
        checkIndexOut(index);//Judge whether it is out of line
        data[index] = o;
        return true;
    }

    /**
     * Determine whether an element exists. If it exists, return the index. If it does not exist, return - 1
     * @param o
     * @return
     */
    public int contains(Object o){
        for(int i = 0,len=data.length;i<len;i++){
            if(o.equals(data[i])){
                return i;
            }
        }
        return -1;
    }

    /**
     * Judge whether the array is empty
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }
}

Posted on Tue, 24 Mar 2020 11:13:00 -0400 by crashmaster