1. For compared to while
The variable for the control loop acts only on the For loop, and the memory is freed after execution. Save Memory 2 compared to while, overloaded function name, parameter list differs from return value regardless of 3,Partition of Memory:(1) register. (2) Local method area. (3) method area. (4) stack memory. Local Variables(5) heap memory. New entities (arrays, objects)4, stack, automatic memory releaseHeap, Java garbage collection mechanism, not timed. 5. Two kinds of exceptions for arraysArrayIndexOutOfBoundsException: When manipulating an array, it accesses the subscript that does not exist in the array. NullPointerException: null pointer exception, when reference does not have any point, the value is null, the reference is also used to manipulate the entity. 6. S.O.P (arr) [[email protected] array, integer type, address, hash value (16 binary) 7, select Sort first turn, maximum value appears in first bit
1 PackageArray;2 Public classArraytest {3 Public Static voidSelectsort (int[] arr)4 {5 for(intX=0; x6 {7 for(intY=x+1; y8 {9 if(arr[x]>Arr[y])Ten { One inttemp=Arr[x]; Aarr[x]=Arr[y]; -arr[y]=temp; - } the } - } - } - Public Static voidMain (string[] args) + { - int[]arr={5,1,6,4,2,8,9}; + //before sorting A PrintArray (arr); at //Sort - Selectsort (arr); - //after sorting - PrintArray (arr); - } - Public Static voidPrintArray (int[] arr) in { -System.out.print ("["); to for(intX=0; x + { - if(x!=arr.length-1) theSystem.out.print (arr[x]+ ","); * Else $ System.out.print (arr[x]);Panax Notoginseng } -System.out.println ("]"); the } +}
8. Bubble sort
First lap, the highest value appears in the last bit
1 Public Static voidBubblesort (int[] arr)2 {3 for(intX=0; x4 {5 for(intY=0; y6 {7 if(arr[x]>Arr[y])8 {9 inttemp=Arr[y];TenArr[y]=arr[y+1]; Onearr[y]=temp; A } -}
9, the fastest sort of hill sort PS: Real development Time Arrays.sort (arr); 10. When an element in an array is exchanged, the element operation of the arrays is not directly on the element operation 11, the operation fails, usually returns-1 12, when the array is searched, when there are multiple key values, gets the position where the key appears in the array for the first time 13, binary lookup premise: Array order
1 PackageArray;2 Public classArrayTest4 {3 Public Static voidMain (string[] args)4 {5 int[] arr={2,4,5,7,19,32,45};6 intIndex=halfsearch (arr,32);7 intIndex_2=halfsearch_2 (arr,2);8System.out.println ("index=" +index);9System.out.println ("index_2=" +index_2);Ten } One Public Static intHalfsearch (int[] arr,intkey) A { - intMin,max,mid; -Min=0; theMax=arr.length-1; -Mid= (Min+max)/2; - while(arr[mid]!=key) - { + if(key>Arr[mid]) -Min=mid+1; + Else if(Key AMax=mid-1; at if(Min>max)//If the value found is greater than the maximum, then min Infinity +1 is greater than Max - return-1; -Mid= (max+min)/2; - } - returnmid; - } in Public Static intHalfsearch_2 (int[] arr,intkey) - { to intMin=0,max=arr.length-1, mid; + while(min<=max) - { theMid= (max+min) >>1; * if(key>Arr[mid]) $Min=mid+1;Panax Notoginseng Else if(Key -Max=mid-1; the Else + returnmid; A } the return-1; + } - $}
14, binary conversion (1) decimal-binary%2/2 (2) decimal-16 binary >>>4
1 PackageArray;2 Public classArrayTest7 {3 Public Static voidMain (string[] args)4 {5ToBin (-6);6ToBa (15);7Tohex (60);8 }9 Public Static voidToBin (intnum)Ten { One //decimal Turn- binary ATrans (num,1,1); - } - the Public Static voidToBa (intnum) - { - //decimal Turn- binary -Trans (num,7,3); + } - + Public Static voidTohex (intnum) A { at //decimal Turn-16 binary -Trans (num,15,4); - } - - - Public Static voidTransintNumintBaseintoffset) in { - if(num==0) to { +SYSTEM.OUT.PRINTLN (0); - return; the } * Char[] chs={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', $' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',Panax Notoginseng' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; - Char[] arr=New Char[32]; the intpos=arr.length; + while(num!=0) A { the inttemp=num&Base; +arr[--pos]=Chs[temp]; -Num=num>>>offset; $ } $ for(intx=pos;x - { - System.out.print (arr[x]); the } -System.out.println ("");Wuyi } the}
15, look up the table method to establish a table, internal storage 0 to F, the table has subscript, can be adjusted with the internal corresponding 16, the character array is defined, the empty space is \u0000 17, two-dimensional array (1) initialized int [][]arr=new INT[3][4];S.O.P (arr);//[[[ Email PROTECTED]S.O.P (arr[0])//[[email protected]----------------------------------int[][]arr=new int[3][];S.O.P (arr[0]);//null
(2) Define one-dimensional array: int[] x; int x[]; two-dimensional array: int[][]y; Int[][]y; Int[]y[];
Java Learning 2 (31-61 Summary)