Linear table (storage structure array)--java implementation

Source: Internet
Author: User
Tags array length

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.