We are right ArrayList Collection of source code for parsing , It belongs to the sequential storage structure of linear data structure , Here we just write the method of adding, deleting, modifying and checking .

First of all, let's take a look ArrayList Data structure of

* The bottom layer is actually an array , When adding elements , Expand the size of the array , Add an element , Capacity increase 1.
* The structure of sequential storage in actual linear storage structure , The element at each location has an index executed , So we can quickly find elements .
Show with simple pictures , as follows :

But when you add elements in the specified location , The elements in the back position move backward uniformly , Low efficiency , The method of copying array of the system will be called here , Because we can't see the source code , So there are ways to study it in depth , The code is as follows :
* The image of the removed element is shown below
// Copy array System.arraycopy(elementData, index+1, elementData, index, numMoved);
Let's have a simple look ArrayList Internal code implementation of
package com.mainshi.mylist; import java.io.Serializable; import
java.util.AbstractList; import java.util.Arrays; import java.util.List; import
java.util.RandomAccess; public class MyArrayList<E> extends AbstractList
implements List,RandomAccess,Cloneable,Serializable{ // First define the member variable // Static constant value ,UID
private static final long serialVersionUID = 8683452581122892189L; // Default capacity
private static final int DEFAULT_CAPACITY = 10; // Empty element data private static final
Object[] EMPTY_ELEMENTDATA = {}; // private static final Object[]
DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; // Array of non serialized objects transient Object[]
elementData; // length private int size; // Construction method public MyArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } public MyArrayList(int
initialCapacity){ // Making legal judgment of input parameters if (initialCapacity > 0) { this.elementData =
new Object[initialCapacity]; } else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA; } else { throw new
IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } }
@Override// How to get the value public Object get(int index) { if(index>=size){ throw new
IndexOutOfBoundsException(); } return elementData[index]; } // Get length @Override
public int size() { return size; } // Method of adding @Override public boolean add(Object
o) { // Methods of capacity expansion ensureCapacityInternal(size + 1); // Assign the new element to the last length
elementData[size++]=o; return true; } // Methods of capacity expansion private void
ensureCapacityInternal(int minCapacity) {
// Ternary operator , If the current collection size is not equal to the default length ( Constructor has confirmation ), The minimum expansion is 0, // If equal , The minimum expansion is 10 int
minExpand=(elementData !=
DEFAULTCAPACITY_EMPTY_ELEMENTDATA)?0:DEFAULT_CAPACITY; // The length of the input is greater than the length of the expansion if
(minCapacity>minExpand){ ensureExplicitCapacity(minCapacity); } } private void
ensureExplicitCapacity(int minCapacity) { modCount++; if
(minCapacity-elementData.length>0){ grow(minCapacity); } }
// A new array is common , The length is greater than the previous array 1; private void grow(int minCapacity) { //
overflow-conscious code int oldCapacity = elementData.length; int newCapacity =
oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0)
newCapacity = minCapacity; // minCapacity is usually close to size, so this is
a win: elementData = Arrays.copyOf(elementData, newCapacity); } // Adds an element at the specified index location
public void add(int index,Object obj){ // Data validity check rangCheck(index); // Expansion
ensureCapacityInternal(size + 1); // Assignment array System.arraycopy(elementData, index,
elementData, index + 1, size - index); // Load the element to the corresponding location elementData[index] = obj;
// Set length increased size++; } private void rangCheck(int index) { if (index>size ||
index<0){ throw new IndexOutOfBoundsException(" Specifies the location of the element "+index+" Length of collection exceeded "); } }
// Method to remove the element at the specified location public E remove(int index){ // Legitimacy check rangCheck(index);
// Monitoring the change of set length modCount++; // Extract the element at the specified location E oldValue = elementData(index);
// The number of elements at the specified location int numMoved = size - index - 1; if (numMoved > 0) // Copy array
System.arraycopy(elementData, index+1, elementData, index, numMoved);
// Let the garbage collector recycle elementData[--size] = null; // clear to let GC do its work
// Returns the removed element return oldValue; } // Methods of extracting elements E elementData(int index) { return (E)
elementData[index]; } // Removes the specified object public boolean remove(Object object){ if
(object==null){// If the execution object is null for (int index=0;index<size;index++){
if(elementData(index)==null){ fastRemove(index); return true; } }
}else{// If you specify an object part null for (int index=0;index<size;index++){
if(elementData(index).equals(object)){ fastRemove(index); return true; } } }
return false; } private void fastRemove(int index) { modCount++; int numMoved =
size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1,
elementData, index, numMoved); elementData[--size] = null; // clear to let GC
do its work } // Modify element placement public Object set(int index,Object object){ // Scope check
rangCheck(index); // Take out the old elements Object oldValue = elementData(index); // Place the new element at the specified location
elementData[index]=object; // Returns the old element return oldValue; } }

©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network