(ii) Use of Java arrays

Source: Internet
Author: User

Java arrays

Unordered array Insert Delete query operation:

 Public classArrayList {Private Static int[] intarray; Private intNelems;  PublicArrayList (intmax) {Intarray=New int[Max]; Nelems=0; }         Public BooleanFindLongSearchkey) {        intJ;  for(j=0;j<nelems;j++){            if(intarray[j]==Searchkey) {                 Break; }        }        if(j==Nelems) {            return false; }Else{            return true; }    }         Public voidInsertintvalue) {Intarray[nelems]=value; Nelems++; }         Public BooleanDeleteintvalue) {        intJ;  for(j=0;j<nelems;j++){            if(value==Intarray[j]) {                 Break; }        }        if(j==Nelems) {            return false; }Else{             for(intk=j;k<nelems;k++) {Intarray[k]=intarray[k+1]; } nelems--; return true; }    }         Public voiddisplay () { for(intj=0;j<nelems;j++) {System.out.print (Intarray[j]+" ");    } System.out.println (); }         Public Static voidMain (string[] args) {intmaxsize=100; ArrayList arr=NewArrayList (maxSize); Arr.insert (77); Arr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33);                Arr.display (); intsearchkey=11; if(Arr.find (Searchkey)) {System.out.println ("Found" +Searchkey); }Else{System.out.println ("Can ' t found" +Searchkey); } arr.delete (55);    Arr.display (); }}

Binary lookup, sequential insertion

//linear Find, two-point lookup Public classOrderList {Private Static int[] intarray; Private intNelems;  PublicOrderList (intmax) {Intarray=New int[Max]; Nelems= 0; }     Public intsize () {returnNelems; }    //Guessing Games//Two-point search     Public intFindintSearchkey) {        intlowerbound = 0; intUpperbound = nElems-1; intCurin;  while(true) {Curin= (lowerbound + upperbound)/2; if(Intarray[curin] = =Searchkey) {                returnCurin; } Else if(Lowerbound >upperbound) {                returnNelems; } Else {                if(Intarray[curin] <Searchkey) {Lowerbound= Curin + 1; } Else{upperbound= CurIn-1; }            }        }    }    //Insert sequentially     Public voidInsertintvalue) {        intJ;  for(j = 0; J < Nelems; J + +) {            if(Intarray[j] >value) {                 Break; }        }         for(intK = Nelems; K > J; k--) {Intarray[k]= Intarray[k-1]; } Intarray[j]=value; Nelems++; }         Public BooleanDeleteintvalue) {        intj=find (value); if(j==Nelems) {            return false; }Else{             for(intk=j;k<nelems;k++) {Intarray[k]=intarray[k+1]; } nelems--; return true; }    }         Public voiddisplay () { for(intj=0;j<nelems;j++) {System.out.print (Intarray[j]+" ");    } System.out.println (); }         Public Static voidMain (string[] args) {intmaxsize=100; OrderList arr=Neworderlist (maxSize); Arr.insert (77); Arr.insert (99); Arr.insert (44); Arr.insert (55); Arr.insert (22); Arr.insert (88); Arr.insert (11); Arr.insert (00); Arr.insert (66); Arr.insert (33);                Arr.display (); intsearchkey=11; if(Arr.find (searchkey)! =arr.size ()) {System.out.println ("Found" +searchkey+ ":" +Arr.find (Searchkey)); }Else{System.out.println ("Can ' t found" +Searchkey); } arr.delete (55);    Arr.display (); }}

Efficiency

Linear Lookup O (N)
Two-point Search O (log N)
Unordered array Insertion O (1)
Sequential array Insertion O (N)
Unordered array Deletion O (N)
Ordered array deletion O (N)

(ii) Use of Java arrays

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.