Reproduced from: http://blog.csdn.net/luoweifu/article/details/8505178
Overview of linear tables
Linear table is the most basic, simplest and most commonly used data structure . The relationship between data elements in a linear table is linear, and data elements can be viewed as being arranged on a line or a ring. The linear table is divided into static linear table and dynamic linear table, common sequential table (static), one-way linked list (dynamic) and bidirectional linked list (dynamic).
The operation of the linear table mainly includes:
( 1 ) to calculate the length of a table N .
(2) Whether the linear table is empty
( 3 adds an element to the end of a linear table
( 4 ) Get the first I elements, 0≤i < n .
( 5 ) clears the linear table
(6) Returns the index of the specified element for the first occurrence in the list, or 1 if the list does not contain this element.
(7) Returns the index of the last occurrence of the specified element in the list, or 1 if the list does not contain this element.
( 8 inserts a new element into the first I a position, 0≤i < n , so that the original first I , i+1 , . .. , n–1 elements into the first i+1 , i+2 , . .. , N elements.
(9) Change the first element ( Ten ) Delete paragraph I elements, 0≤i < n , so that the original first i+1 , i+2 , . .. , n–1 elements into the first I , i+1 , . .. , n–2 an element
Thus, the list interface is defined for the abstract data type of the linear table as follows: list interface
[Java] View plain copy package list; public interface list { /** * add elements to the end of a linear table */ public void add (object e); /** * Clear Linear table */ public void clear (); /** * Get i location elements * @param i * @return */ public object get (int i); /** * Returns the index of the specified element for the first occurrence in the list, and returns &NBSP;-1 if the list does not contain this element. &NBSP;&NBsp; * @param e * @return */ public int indexof (object e); /** * Insert an element after I e * @param i * @param e */ public void insert (int i, object e); /** * determine if the linear table is empty * @return */ public boolean isempty (); /** * returns the index of the specified element at the end of the list, or -1 if the list does not contain this element. * @param e * @return */ public int lastindexof (object e); /** * remove elements from the specified position in the list * @param i */ public void remove (int i); /** * Replaces the element at the specified position in the list with the specified element (optional operation). * @param i * @param e */ public void set (int i, object e); /** * Returns the size of the linear table * @return */ &NBSP; public int size (); }
Order Table Structure Model
Diagram sequence storage structure memory structure diagram
Source Code [Java] View plain copy package list; public class arraylist implements list{ /** * Sequence table default Initial size */ public static final int deflen = 10; private int maxLen; private Object[] array; private int size; public arraylist () { size = 0; maxLen = defLen; array = new Object[defLen]; } @Override public void Clear () { size = 0; } @Override public object get (int i) { if (i>=0 && i<size) return array[i]; else return null ; } @Override public int indexof (object e) { int i =0; while (i<size && !array[i].equals (e)) { i++; } if (i < size) return i; else return -1; } @Override public void insert (int i, object e) { if (i >= size) { i = size; if (size) >= maxlen)//If the position of the insertion number is greater than the maximum capacity of the order table, expand the capacity expand (); } for (int j = size; j>i+1; j--) { array[j] = array[j-1]; } array[i+1] = e; size ++; } @Override Public boolean&nbsP;isempty () { if (size == 0) return true; else return false; } @Override public int lastindexof ( Object e) { int i = size-1 ; while (i>=0 && !array[i). Equals (e)) { i--; } if (i>=0) return i; else return -1; } @Override public void remove (int i) { for (int j=i; j<size-1; j++) { array[j] = array[j+1]; } size --; } @Override public void set (Int i, object&nbSp;e) { array[i] = e; } @Override public int size () { return size; } /** * size of the sequential table when the size of the order list is not enough */ private void expand () { maxLen = 2*maxLen; Object newarray[] = new