ArrayList source code learning of linear table of data structure

I haven't written a blog for a long time. I've been writing it on my notes.

I always use JDK8, so today's source code analysis is also based on JDK8.

 

Introduction to ArrayList

 

ArrayList is a dynamic array queue. Compared with arrays in Java, its capacity can grow dynamically. It inherits from AbstractList and implements the interfaces of list, randomaccess, Cloneable and java.io.serializable.

 

 


The father of ArrayList is AbstractList, which implements List. AbstractList provides functions of adding, deleting, modifying and traversing.


ArrayList implements the RandmoAccess interface and supports fast random access.
ArrayList implements the clonable interface, which covers the function clone(), and can be cloned.
ArrayList implements java.io.Serializable interface, which means that ArrayList supports serialization and can be transmitted through serialization.

Unlike Vector, operations in ArrayList are not thread safe! Therefore, it is recommended to use ArrayList in a single thread, while Vector or CopyOnWriteArrayList can be selected in multithreading.

 

Properties of ArrayList

  1. Serialization of id
  2. Default initial capacity
  3. Array length

  4. .........

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable {
	// Serialization of id
	private static final long serialVersionUID = 8683452581122892189L;
	// Initial capacity (different from Android Java, Android is 12)
	private static final int DEFAULT_CAPACITY = 10;
	// An empty object
	private static final Object[] EMPTY_ELEMENTDATA = new Object[0];
	// An empty object, if created using the default constructor, the default object content is this value by default
	private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = new Object[0];
	// Where the current data object is stored. The current object does not participate in serialization
	transient Object[] elementData;
	// Current array length
	private int size;
	// Array maximum length
	private static final int MAX_ARRAY_SIZE = 2147483639;
 
	
}

ArrayList constructor

 

  • Default constructor

 

 public ArrayList() {
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

//When add ing for the first time, elementData will become the default increment length: 10
  • int type parameter constructor

 public ArrayList(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);
        }
    }
  • Parameter constructor of type Collection

public ArrayList(Collection<? extends E> c) {

      //Convert the collection object to an array, and assign the address of the array to elementData.
        elementData = c.toArray();
      
        //If its length is not 0, then judge whether it is an array of Object type. If not, give it copyOf() as an array of Object type. Otherwise, leave it empty.

        if ((size = elementData.length) != 0) {
          
            if (elementData.getClass() != Object[].class)
                elementData = Arrays.copyOf(elementData, size, Object[].class);
        } else {
            // replace with empty array.
            this.elementData = EMPTY_ELEMENTDATA;
        }
    }

Method of ArrayList

 

Insert data: add() method

 

add method:

  1. Make sure that the size of the array plus 1 is enough to hold the next data
  2. The number of modifications of modCount is increased by 1. If the used length of the current array (size) plus 1 is greater than the current array length, the grow method is called to grow the array. The grow method will change the length of the current array to 1.5 times the original capacity.
  3. After making sure that the new data has a place to store, add the new element to the location at size.
  4. Returns the Boolean value of adding success.

Insert data at specified location: add(int index, E element) method

 

 

 

Add elements in the specified location: first, rangeCheckForAdd(index) determines whether the index exceeds the array subscript, and then checks whether expansion is needed. Use System.arraycopy to move all elements after the location (index) to be inserted one bit backward. Store the new data content in the specified location (index) of the array.

 

 

get method: returns the element at the specified location

 

 

 

 

 

 

 

32 original articles published, 97 praised, 550000 visitors+
Private letter follow

Tags: Java Android

Posted on Sun, 12 Jan 2020 11:41:41 -0500 by wardmaestro