One, foreword

The order table is a common kind, learning and understanding is very important, the order table for the future learning to lay a foundation.

Second, the definition of order

Sequential represents a linear table held as an array in computer memory, occupying a contiguous set of storage

Unit, in which each element is stored in turn.

Three, the implementation of the order table

3.1 API design of sequence table

3.2 Code implementation of the sequence table

Define a generic class (the nice thing about generic classes is that they can accept any type)

Public class SequenceList<T> {}Copy the code

Define member variables in a generic class

Private T[] eles; private T[] eles; Private int N; private int N;Copy the code

Defines a constructor used to initialize a member variable

Public SequenceList(int capacity) {this.eles = (T[]) new Object[capacity]; this.eles = (T[]) new Object[capacity]; This.n = 0; this.n = 0; this.n = 0; }Copy the code

The following functions are implemented:

Sets linear tables to empty tables

Public void clear(){this.n =0; this.n =0; this.n =0; } // The reason we use this is that it must refer to a member variable to prevent local variables from having the same name as member variables. // Use this whenever a member variable is involvedCopy the code

Check whether a linear table is empty

Public Boolean isEmpty(){return this.N==0; return this.N==0; }Copy the code

Gets the length of the linear table

Public int length(){this.n; }Copy the code

Gets the element at position I

Return eles[I]; public T get(int I){return eles[I]; }Copy the code

Add element T to the linear table

Public void insert(t t){eles[N++]=t; eles[N++]=t; // This expression is equivalent to eles[N]=t; N++; }Copy the code

Insert element t at index I

Public void insert(int I, t t){for (int index=N; index>i; Eles [index]=eles[index-1]; Eles [I]=t; N++; }Copy the code

Insert schematic diagram:

Removes the element at the specified position I and returns the element

Public T remove(int I){T current=eles[I]; T current=eles[I]; For (int index=0; index<N-1; Eles [index]=eles[index+1]; } // The number of elements is reduced by 1, return the value N-- overridden by the (I); return current; }Copy the code

Returns the first occurrence of the element t

Public int indexOf(t t){for (int I =0; i<N; i++){ if(eles[i].equals(t)){ return i; }} // return -1; }Copy the code

3.3 Complete API Overview:

Public class SequenceList<T> {private T[] eles; public class SequenceList<T> {private T[] eles; Private int N; private int N; Public SequenceList(int capacity) {this.eles = (T[]) new Object[capacity]; this.eles = (T[]) new Object[capacity]; This.n = 0; this.n = 0; this.n = 0; } public void clear(){this.n =0; // The reason we use this is that it must refer to a member variable to prevent local variables from having the same name as member variables. } public Boolean isEmpty(){return this.N==0; return this.N==0; } public int length(){this.n; } public T get(int I){return eles[I]; } public void insert(t t){eles[N++]=t; eles[N++]=t; // This expression is equivalent to eles[N]=t; N++; } public void insert(int I, t t){for (int index=N; index>i; Eles [index]=eles[index-1]; } // add t to index I and add 1 N++; eles[i]=t; } public T remove(int I){T current=eles[I]; For (int index=0; index<N-1; Eles [index]=eles[index+1]; } // The number of elements is reduced by 1, return the overridden value N--; return current; Public int indexOf(t t){for (int I =0; i<N; i++){ if(eles[i].equals(t)){ return i; }} // return -1; }}Copy the code

Four, the test of the sequence table:

Public class SequenceListText {public static void main(String[] args) {SequenceList<String> sl=new SequenceList<String>(10); // Insert element sl.insert(" hole "); Sl. Insert (" liu Shijun "); Sl. Insert (0," sun "); String s=sl.get(0); System.out.println(s); String remove1=sl.remove(0); System.out.println(remove1); // Clear the element sl.clear(); System.out.println(sl.length()); / / 0}}Copy the code

\