1 /*array implementations of linear tables2 * Features: Insert delete slow need to average move half of data, find faster3 * Note: There will be some differences in the operation of duplicate and no duplicate data4 * Note that once the array is created it is fixed5 *java Collection length is variable because creating a new array copies the old data in the past--these additional functions sacrifice efficiency6 * The main implementation of the following several functions7 * * Linear table Lookup8 Linear Table Delete9 the insertion of a linear table stores an element (unordered array insertion)Ten * */ One Public classMyArray { A - Private Long[] arr; - Private intItems//record the number of array elements-key the - PublicMyArray (intMax) {//Max record array length - This. arr =New Long[Max]; -Items = 0; + } - + Public intsize () { A returnitems; at } - //Find by Value - Public BooleanFindLongSearchkey) { - intindex = 0; - for(inti = 0; I < items; i++){ - if(Searchkey = =Arr[i]) { inindex =i; - Break; to } + } - if(Index = =items) { the return false; * } $ Else{Panax Notoginseng return true; - } the } + A //Storage Elements the Public voidInsertLongkey) { +Arr[items] =key; -items++; $ } $ - //Delete an element-by value - Public BooleanDeleteLongkey) { the //iterate through an array--find the element first - inti;Wuyi for(i = 0; i < items; i++){ the if(Key = =Arr[i]) { - Break; Wu } - } About if(i==items) { $ return false; - } - Else{ - //move action to the left A for(intK = i;k < items; k++){ +Arr[k] = arr[k+1]; the } -items--; $ return true; the } the } the the Public voidDisplayarray () { - for(inti = 0;i < items;i++){ inSystem.out.print (Arr[i] + ""); the } the System.out.println (); About } the}
Linear table (storage structure array)--java implementation