The commonly used data structures are divided into linear and nonlinear structures, while linear structures include tables, stacks, queues, nonlinearity including trees, graphs, and so on. According to data storage methods can be divided into sequential tables and linked lists, stacks are divided into sequential stacks, chain stacks, queues can also have a chain is a queue. In high-level languages, arrays are typically used to represent sequential storage structures, so tables, stacks, and queues can be used in arrays.
1 PackagearrayList;2 3 Importjava.util.Arrays;4 Importjava.util.Date;5 6 /**7 * Sequential table ArrayList, expressed in arrays. A contiguous set of address spaces8 * @authorlh-pc9 * @param<E>Ten */ One Public classArraylist<e> { A Privateobject[] data =NULL;//data fields used to hold this linear table - Private intLength//capacity of linear table - Private intCurrent//actual table Length the - /** - * The size is set to ten by default - */ + PublicArrayList () { - This(10); + } A at - /** - * Initialize the linear table, declare the size of the array - * @paraminitialsize Array Size - */ - PublicArrayList (intinitialsize) { in if(initialsize >= 0){ - This. length = InitialSize;//set the linear table capacity to This. data =NewObject[initialsize];//initializing an array + This. current = 0;//Subscript set to 0 -}Else { the Throw NewRuntimeException ("Initialization size cannot be less than 0:" + initialsize);//Exception Prompt * } $ Panax Notoginseng } - the + /** A * Add elements at the end of the linear table to determine if the linear table is full before adding the * @parame-Added elements + * @returnSuccessful return True - */ $ Public BooleanAdd (e e) { $ //determine if it is full - ensurecapacity (); - //add an element to the end of an array the This. data[current++] =e; - //++current;//Subscript + +Wuyi return true; the } - Wu /** - * Delete the element at the specified location About * @paramIndex $ * @return - */ - Public BooleanRemovetoindex (intindex) { - //Delete the elements of an array: Use to change the array subscript to achieve the effect of deletion. A //iterates over array matches the specified subscript, allowing the element to the right of the specified subscript to move to the left to change the subscript. And finally, the rightmost subscript is deleted. + //a b c the //0 1 2 - //Data[index] = data[index + 1]; //change the subscript on the right $ //Data//Delete the rightmost subscript the //start the traversal from the subscript you want to delete and move the right element to the left the if(Index >= current) {//returns False if Index is greater than the maximum length theSystem.err.print (NewDate () + ": Subscript exceeds table Length"); the return false; - } in for(inti = index; i < current-1; i++) { theData[i] = data[i+1];//the Table element subscript the } AboutDATA[CURRENT-1] =NULL;//changes the rightmost element of the original subscript to null the--current;//actual table length -1 the return true; the } + - the /**Bayi * Returns the element value according to the subscript the * @paramIndex the * @return - */ - PublicE Get (intindex) { the if(Index >= 0){ the return(E) data[index]; the}Else { the Throw NewRuntimeException ("Subscript cannot be less than 0:" +index); - } the } the the /**94 * Determine if the table capacity exceeds the predetermined size, if the excess will automatically expand the capacity the * the */ the Public voidensurecapacity () {98 //determine if the actual length of the table exceeds the maximum table capacity About if(Current >=length) { -Length *= 2;//Maximum table capacity101data = arrays.copyof (data, length);//Copy the original array102 }103 104 } the 106 107 /**108 * Returns the actual table length of the order table109 * @return the */111 Public intsize () { the return This. Current;113 } the the /** the * Return table capacity117 * @return118 */119 Public intLength () { - return This. Length;121 }122 123 /**124 * the * Determine if the table is empty126 * @paramargs127 */ - Public BooleanIsEmpty () {129 //return (current = = 0)? true:false; the returncurrent = = 0;//if current = = 0, the description is empty to return true, otherwise return false131 } the 133 //Main Method Test134 Public Static voidMain (string[] args) {135arraylist<integer> list =NewArraylist<integer> ();//Create ArrayList136 for(inti = 1; I <= 22; i++) {137 List.add (i);138 }139List.removetoindex (0); $ //List.removetoindex (List.size ());141 //iterating through the list array142 for(inti = 0; I < list.size (); i++) {143 System.out.println (List.get (i));144 }145 146 }147 148}
Java Implementation Order table