(4) Array common operations
A. Traversing values
classarraydemo3{ Public Static voidMain (string[] args) {//System.out.println ("Hello world!"); //Format 1 /*a container is required, but the specific data of the container is not clear. */ //int[] arr = new Int[8]; //Format 2 /*a container is needed to store the specific data that has been kept. */ //element type [] Array name = new element type [] (element, element, ...); //int[] arr = new int[]{12,25,58,69}; int[] arr = {12,25,58,69};//relatively simple notation /*The most basic function of an array operation is to save and fetch. The core idea: is the operation of the diagonal mark. */ //System.out.println (arr[0]); //System.out.println (arr[1]); //System.out.println (arr[2]); //System.out.println (arr[3]); //System.out.println (arr.length);//the length of the arr array for(intx=0;x<arr.length; x + +) {
System.out.println ("arr[" +x+ "]=" +arr[x]+ ";"); } }}
B. Get maximum value (max, min)
classarraydemo4{ Public Static voidMain (string[] args) {int[] arr = {25,56,35,97,39}; intMax =Getmax (arr); System.out.println ("Max=" +max); } /*gets the maximum value in the array 1. A comparison is required and the variable records a larger value after each comparison. 2. The elements in the array are traversed and taken out, compared to the elements recorded in the variable. If the traversed element is larger than the element recorded in the variable, the variable is used to record the larger value. 3. The loop ends, and the variable record is the maximum value. Define a feature to implement. Explicit one, the result is an element in the array. Explicit two, position array of contents. */ Public Static intGetmax (int[] arr) { //defines a large value for a variable record. intMaxelement = arr[0]; for(int× = 0;x<arr.length; x + +) { if(arr[x]>maxelement) {maxelement=Arr[x]; } } returnmaxelement; } Public Static intGetmax_2 (int[] arr) { //defines a large value for a variable record. intMaxindex = 0; for(int× = 0;x<arr.length; x + +) { if(arr[x]>Arr[maxindex]) {Maxindex=x; } } returnArr[maxindex]; }}
C. Sorting (select Sort, bubble sort)
classarraydemo4{ Public Static voidMain (string[] args) {int[] arr = {25,56,35,97,39}; Selectsort (arr); } /*Select Sort*/ Public Static voidSelectsort (int[] arr) { for(intx=0;x<arr.length-1;x++) { for(inty=x+1;y<arr.length;y++) { if(arr[x]>Arr[y]) { inttemp =Arr[x]; ARR[X]=Arr[y]; Arr[y]=temp; } }}}/*Bubble Sort*/ Public Static voidBubblesort (int[] arr) { for(intx=0;x<arr.length;x++) { for(inty=0;y<arr.length-1-x;y++) { if(arr[y]>arr[y+1]) { inttemp =Arr[y]; Arr[y]= Arr[y+1]; Arr[y+1] =temp; } } } }}
D. Split semi-search (binary search)
(5) array in array
classarraydemo5{ Public Static voidMain (string[] args) {//int [] arr = {5,8,12,54,23,69,52}; //int index = GetIndex (arr,619); //int [] arr = {5,8,12,23,39,69,95}; //int index = halfsearch_2 (arr,70); //System.out.println (index); //int index1 = Arrays.binarysearch (arr,12);//if present, returns the specific corner mark, if not present, returns the-insertion point-1;//System.out.println ("index1=" +index1);Tohex_1 (60); //System.out.println ("index1=" +index1); } /*Two-Part search method*/ Public Static intHalfsearch (int[] arr,intkey) { intMax,min,mid; Min= 0; Max= Arr.length-1; Mid= (Min+max)/2; while(Arr[mid]! =key) { if(key>Arr[mid]) {min= Mid+1; }Else{Max= Mid-1; } if(max<min) { return-1; } Mid= (Min+max)/2; } returnmid; } Public Static intHalfsearch_2 (int[] arr,intkey) { intMax,min,mid; Min= 0; Max= Arr.length-1; while(min<max) {Mid= (Min+max) >>1;//The right shift is 1 bits except 2. if(key>Arr[mid]) min= Mid+1; Else if(key<Arr[mid]) Max= Mid-1; Else returnmid; } return-1; } /*Array Common features: Find. If there are two destination elements in the array, the index that contains the first element is returned. */ /*public static int getindex (int arr[],int key) {for (int x=0;x<arr.length;x++) {if (arr[x] = = Ke Y) {return x; }} return-1; }*/ /*when do you use arrays? If the data has a corresponding relationship, and the corresponding party is an ordered number of numbers, and used as a corner label, then it is necessary to think of the use of arrays. You can store this data in an array. The corresponding elements in the array can be checked directly according to the result of the operation as a corner mark. This way: called the look-up table method. */ Public Static voidTohex_1 (intnum) { //define a corresponding relational table Char[] CHS = {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; for(intx=0;x<8;x++) { inttemp = num & 15; System.out.print (Chs[temp]); Num= num >>>4; } } Public Static voidTohex_2 (intnum) { //define a corresponding relational table Char[] CHS = {' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; /*a look at the table will find more data data more than one, first saved up, and then the operation. So define an array, a temporary container*/ Char[] arr =New Char[8]; intpos = 0; while(num! = 0) { intTamp = num&15; Arr[pos++] =Chs[temp]; Num= num >>>4; } } }
Java Learning Day No. 05 (array common operations, arrays in arrays)