Array of array structures

Source: Internet
Author: User

The use of the array of data structures is nothing more than adding and removing the operation, on the ordered array and unordered array for these three operations:

First, find

(1) Unordered array finds specific elements, linear lookup:

1  Public Static voidUnsortsearchkey (intArr[],intkey) {2          for(inti = 0; i < arr.length; i++) {3             if(arr[i]==key) {4System.out.println (key+ "+i+" position "indexed in unordered array");5             }6             Else {7                 Continue;8             }9             if(i==arr.length) {TenSystem.out.println (key+ "does not exist in an unordered array"); One             } A         } -  -}

(2) ordered array to find specific elements, can be linear or binary lookup (BinarySearch), here we use the binary search array for ascending or descending order, using two points to find code code some different, pit dad

1  Public Static intSortsearchkey (int[] Arrsort,intkey) {2         3         intMax,min,mid; 4Min = 0; 5max = Arrsort.length-1; 6            while(min<=max)7           {  8Mid = (max+min) >>1; 9            if(key>Arrsort[mid])Tenmax = Mid-1;  One            Else if(key<Arrsort[mid]) AMin = mid + 1;  -            Else   -             returnmid;  the           }   -           return-min-1;  -}

Second, insert

(1) Inserts an element into an ordered array and returns an array after inserting the element:

1  Public Static int[] Insertkey (int[] Arrsort,intKey_insert) {2 //int left=0;3 //int right=arrsort.length-1;4 //int half= (left+right)/2;5         intposition=Sortsearchkey (Arrsort, key_insert);6         if(position>=0){7Arrsort=arrays.copyof (Arrsort, arrsort.length+1);8              for(intx=arrsort.length-1;x>position;x--){9Arrsort[x]=arrsort[x-1];Ten             } Onearrsort[position]=Key_insert; A //Displayarr (arrsort); -         } -         Else{ theArrsort=arrays.copyof (Arrsort, arrsort.length+1); -              for(intx=arrsort.length-1;x>=-(position+1); x--){ -Arrsort[x]=arrsort[x-1]; -             } +arrsort[-(position+1)]=Key_insert; - //Displaysortarr (arrsort); +         } A         returnArrsort; at}

(2) Inserting elements in an unordered array (array expansion, inserting to the end of the array)

1  Public Static void insertkey_2 (intint  key_insert) {2         arrays.copyof (arr, Arr.length+1); 3         arr[arr.length-1]=Key_insert; 4         Displayarr (arr); 5     }

Third, delete the element

1      Public  Static voidDelete_1 (intArr[],intDelsortindexkey) {2         if(delsortindexkey>=arr.length) {3             return;4         }5          for(inti=delsortindexkey;i<arr.length-1;i++){6Arr[i]=arr[i+1];7         }8Arr[arr.length-1]=0;9 Displaysortarr (arr);Ten}

Array of array structures

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.