Handwritten ArrayList core source code
public class QList<T> { //The size of the default array private final int DEFAULT_LIST_SIZE = 8; //Where to store data private Object[] mData; //Index of the next current array that can store data private int mSize; ...... }
public class QArrayList<T> { //The size of the default array private final int DEFAULT_LIST_SIZE = 8; //Where to store data private Object[] mData; //Index of the next current array that can store data private int mSize; public QArrayList() { //New An array used to store mData = new Object[DEFAULT_LIST_SIZE]; //The index of the next current array that can store data is 0 mSize = 0; } public QArrayList(int capacity){ if(capacity <= 0 || capacity > Integer. MAX_VALUE){ throw new RuntimeException("invalid capacity"); } mData = new Object[capacity]; mSize = 0; } //Returns how many elements have been stored in the array at that time public int size() { return mSize; } //Returns the total size of the array. In fact, it is not necessary to provide this interface externally. Here we are just for demonstration public int capacity() { return mData.length; } //Add an element public void add(T e) { //It is not allowed to add an empty element if(e == null){ return; } //If the current array is full, it will be expanded twice as much as the original array if (mSize >= mData.length) { //Capacity expansion resize(); } //Add the added element to the array mData[mSize] = e; //At the same time, mSize++points to the next location where data can be stored mSize++; } //Get the element at the specified position. If the position is illegal, throw an exception directly //This is necessary. What we provide is a library //Throw an exception directly to let users know that it is wrong. There is no need to return null //Because this is a library, not a business. Even if the return is null, it is also a business level issue public T get(int position) { if (position < 0 || position >= mData.length) { throw new RuntimeException("position is invalid"); } //It doesn't matter if the position is greater than mSize, because it also returns null, proving that it has not been obtained return (T) mData[position]; } //Delete the element at the specified position public T remove(int position) { //As above, the illegal location directly throws an exception if (position < 0 || position >= mData.length) { throw new RuntimeException("position is invalid"); } //Save the element to be deleted and return the element to be deleted T e = (T) mData[position]; //After deletion, move all the following elements forward for (int i = position + 1; i < mData.length; i++) { mData[i - 1] = mData[i]; } //Don't forget that mSize needs-- mSize--; //Return deleted elements return e; } //Delete the specified element public boolean remove(T e) { //Because the array may not be full, if the deletion is null, it is unnecessary. We do not allow if (e == null) { return false; } //Find where to delete the element int position = -1; for (int i = 0; i < mData.length; i++) { if (e == mData[i] || e.equals(mData[i])) { position = i; break; } } //Return if not found if (position == -1) { return false; } //Delete return remove(position) != null; } //Capacity expansion, we all expand the capacity by twice private void resize() { Object[] old = mData; mData = new Object[mData.length * 2]; for (int i = 0; i < old.length; i++) { mData[i] = old[i]; } old = null; } }
public static void main(String[] args) { QArrayList<String> list = new QArrayList<>(); list.add("tom"); list.add("jim"); list.add("lilei"); list.add("hanmeimei"); System.out.println("list.get(2)=" + list.get(2)); System.out.println("list.size()=" + list.size()); for (int i = 0; i < list.size(); i++) { System.out.println("list.get(" + i + ") = " + list.get(i)); } System.out.println("======================="); System. out. println ("Demonstrate deletion"); list.remove("jim"); for (int i = 0; i < list.size(); i++) { System.out.println("list.get(" + i + ") = " + list.get(i)); } }
public static void main(String[] args) { //Create a new array with only 2 elements QArrayList<String> list = new QArrayList<>(2); //Print the expanded capacity System. out. println ("before capacity expansion: list. capacity()="+list. capacity()); //We added 4 elements list.add("tom"); list.add("jim"); list.add("lilei"); list.add("hanmeimei"); //Print the expanded capacity System. out. println ("After capacity expansion: list. capacity()="+list. capacity()); //Print for (int i = 0; i < list.size(); i++) { System.out.println("list.get(" + i + ") = " + list.get(i)); } }