The sequential table represented by the sequential storage structure is called the sequential table.
It stores data elements in a linear table at once with a contiguous set of address storage units.
The implementation of sequential tables is the simplest of the data structures.
The code is no longer elaborated because it has been commented out in detail.
Next time, the code for the loop queue and the sequential stack of the sequential table is presented.
1 Packagelinear tables-sequential tables-normal arrays;2 3 /**4 * ArrayList sequence table5 * JAVA 3.06 * Subscript for throwing exception handling errors7 * Use generics, of course, if you want to replace the object can also be replaced8 */9 Public classArraylist<e> {Ten One Private intcapacity;//total capacity of the array A Private intCurrent//The next element of the current last array element is subscript, starting with 0, also the length - Privatee[] data =NULL;//All Data - the PublicArrayList () { - - This(10); - } + - PublicArrayList (intinitialsize) { + A init (initialsize); at } - /** - * Initialize Array - */ -@SuppressWarnings ("Unchecked") - Public voidInitintinitialsize) { in -Current = 0; todata = (e[])NewObject[initialsize]; +Capacity =initialsize; - } the /** * * Insert element at end of array*/ $ Public voidAdd (e e) {Panax Notoginseng - ensurecapacity (); theData[current] =e; +current++; A the } + /** - * Inserting elements in an array*/ $ Public voidInsertintindex, E E) { $ - Validateindex (index); - ensurecapacity (); the for(inti=current;i>=index;i--){ - WuyiDATA[I+1] =Data[i]; the } -Data[index] =e; Wucurrent++; - } About /** $ * Determine if you need to extend an array - * If you need to expand to twice times the original - */ -@SuppressWarnings ("Unchecked") A Private voidensurecapacity () { + the if(Current = =capacity) { - $Capacity = capacity * 2; theE[] NewData = (e[])NewObject[capacity]; the for(intindex = 0; Index < current; ++index) { theNewdata[index] =Data[index]; the } -data =NewData; in } the } the About /** the * Delete an object that already exists the * If T is in the array, then the delete succeeds returns True, otherwise none of this element returns false the */ + Public BooleanRemove (e e) { - the for(inti=0;i<current;i++){Bayi the if(Data[i].equals (e)) { the - Remove (i); - return true; the } the } the return false; the } - /** the * Delete an array of specific subscripts the * @paramIndex the */94 Public voidRemoveintindex) { the the Validateindex (index); the for(inti=index;i<current;i++){98 AboutData[i] = data[i+1]; - }101Data[current] =NULL;102current--;103 }104 /** the * Change the value of subscript to index*/106 Public voidSetintindex, E E) {107 108 Validateindex (index);109Data[index] =e; the }111 /** the * Gets the value of subscript as index113 */ the PublicE Get (intindex) { the the Validateindex (index);117 returnData[index];118 }119 /** - * Gets the number of arrays already used121 */122 Public intsize () {123 124 returnCurrent ; the }126 /**127 * Destroy all elements of an array - */129 Public voidClear () { the 131 //arrays.fill (data, NULL); Can replace the following for loop the for(inti=0;i<current;i++){133 134Data[i] =NULL;135 }136Capacity = 0;137Current = 0;138 }139 /** $ * Determine if the array is empty141 */142 Public BooleanIsEmpty () {143 144 returnCurrent==0;145 }146 /**147 * Print Structure148 */149 PublicString toString () { Max 151String str = "["; the for(Object o:data) {153 if(O! =NULL) { 154str + = O + ""; 155 } 156 } 157str + = "]"; 158 returnstr; 159 } the /*** Determine if index is out of bounds*/161 Private voidValidateindex (intindex) { 162 163 if(Index < 0 | | | Index >=Current ) { 164 Throw NewIndexoutofboundsexception ("Invalid subscript:" +index); 165 } 166 } 167
Java Implementation Order table