--java implementation of ordered linear table (storage structure array)

Source: Internet
Author: User

1 /*ordered arrays: mainly to improve the efficiency of search2 * Find: Unordered array--order lookup, ordered array--binary find3 * where insertion is slower than unordered array4  * */5  Public classMyorderedarray {6     Private Long[] arr;7     Private intitems;8     9      PublicMyorderedarray (intmax) {Tenarr =New Long[Max]; OneItems = 0; A     } -      -     //the number of elements in the array the      Public intsize () { -         returnitems; -     } -      +     //binary Find -      Public intFindLongkey) { +         intLow = 0; A         intHigh = Items-1; at         intMid//changes in -          while(Low <=High ) { -Mid = (low + high)/2; -             if(Key = =Arr[mid]) { -                 returnmid; -             } in             Else if(Key >Arr[mid]) { -Low = mid + 1; to             } +             Else if(Key <Arr[mid]) { -High = Mid-1; the             } *         } $         return-1;Panax Notoginseng     } -      the     //inserting elements +      Public voidInsertLongkey) { A         inti; the          +          for(i = 0; i < items; i++) {//Find your location first -             if(Key <Arr[i]) { $                  Break; $             } -         } -          the          for(intK = items;k>i; k--) {//Moving Elements -Arr[k] = arr[k-1];Wuyi         } the          -Arr[i] = key;//insert element to location found Wuitems++; -     } About      $     //Delete Element--first judge the position -      Public BooleanDeleteLongkey) { -         inti =find (key); -         if(i = =items) { A             return false; +         } the         Else{ -              for(intK = i; K < items; k++){ $Arr[k] = arr[k + 1]; the             } theitems--; the             return true; the         } -     } in      the      Public voidDisplayarray () { the          for(inti = 0;i < items;i++){ AboutSystem.out.print (Arr[i] + ""); the         } the System.out.println (); the     } +      -}

--java implementation of ordered linear table (storage structure array)

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.