Java linear table sequence implementation, java linear sequence implementation
I wrote a Simplified Linear table like arrayList. It is mainly used to study what operations arrayList can achieve to save performance. The author has a poor literary talent and directly uses the code.
Import java. util. arrays; public class SequenceList <T> {private final int DEFAULT_SIZE = 16; // Save the length of the array private int capacity; // define an array to save the private Object [] elementData element of the ordered linear table; // Save the current number of elements in the ordered linear table. private int size = 0; // create an empty ordered linear table named public SequenceList () {capacity = DEFAULT_SIZE; elementData = new Object [capacity];} // create an ordered linear table public SequenceList (T element) {this (); elementData [0] = element; size ++ ;} /*** create an ordered linear table with an array of the specified length ** @ param element * specify the first element in the ordered linear table * @ param initSize * specify the length of the underlying array of the ordered linear table */ public SequenceList (T element, int initSize) {capacity = 1; // set capacity to the n while (capacity <initSize) {capacity <= 1 ;} elementData = new Object [capacity]; elementData [0] = element; size ++;} // obtain the size of the ordered linear table public int length () {return size ;} // obtain the public T get (int I) {if (I <0 | I> size-1) element at the index of the ordered linear table) {throw new IndexOutOfBoundsException ("linear table Index out-of-bounds");} return (T) elementData [I];} // query the public int locate (T element) index of the specified element in the ordered linear table) {for (int I = 0; I <size; I ++) {if (elementData [I]. equals (element) {return I ;}} return-1 ;}// insert an element public void insert (T element, int index) to the specified position of the ordered linear table) {if (index <0 | index> size) {throw new IndexOutOfBoundsException ("linear table index out of bounds");} ensureCapacity (size + 1 ); // move all elements after the specified index to a System. arraycopy (elementData, index, elementData, index + 1, size-index); elementData [index] = element; size ++ ;} // add an element public void add (T element) {insert (element, size) at the beginning of the linear sequence table;} // very troublesome, and poor performance private void ensureCapacity (int minCapacity) {// if the original length of the array is smaller than the current length if (minCapacity> capacity) {// continuously change capacity * 2, until capacity is greater than minCapacitywhile (capacity <minCapacity) {capacity <= 1;} elementData = Arrays. copyOf (elementData, capacity) ;}// delete the public T delete (int index) element of the specified index in the ordered linear table) {if (index <0 | index> size-1) {throw new IndexOutOfBoundsException ("linear table index out of bounds");} T oldValue = (T) elementData [index]; int numMoved = size-index-1; if (numMoved> 0) {System. arraycopy (elementData, index + 1, elementData, index, numMoved);} // clear the last element elementData [-- size] = null; return oldValue ;} // delete the last element of the ordered linear table, public T remove () {return delete (size-1);} // determine whether the ordered linear table is empty. public boolean empty () {return size = 0;} // clear the linear table public void clear () {// assign all elements of the underlying array to nullArrays. fill (elementData, null); size = 0;} public String toString () {if (size = 0) {return "[]";} else {StringBuilder sb = new StringBuilder ("["); for (int I = 0; I <size; I ++) {sb. append (elementData [I]. toString () + ",");} int len = sb. length (); return sb. delete (len-2, len ). append ("]"). toString ();}}}
Ordered tables use arrays to store data, so they have good performance support for random access. No matter which element of a linear table is accessed, elementData [I] can be directly obtained, however, adding an element consumes a lot of performance. The main reason is that when an element is inserted randomly, the following element may be moved to the backend, when the array length is not enough, you need to create a new array of 2 times the original array, and then migrate the data to the new array as a whole, and then release the original array, which consumes a lot of performance, therefore, the use of arrayList usually involves no complicated insert operations, but more operations on data, while the use of sorted list (linked list) has the opposite performance.