element type [] Array name = new element type [element number or array length];
Array is a reference data type |-array data type
|
Memory structure: When the program is running, it needs to allocate space in memory. In order to improve the efficiency of operation,
There are different areas of space, because each region has a specific way to process data and
Memory memory management mode.
Stack memory: Used to store local variables, and the occupied space is automatically freed when the data is used.
Heap Memory: 1, arrays and objects, instances created by new are stored in heap memory
2, each entity has a memory address value.
3, the variables in the entity have default initial values.
4, entities are not being used and will be reclaimed by the garbage collector at an indeterminate time.
Default initialization value in heap memory: int is 0, double is 0.0, Boolean is False
Method area
Local Method Area
Register
garbage collection mechanism [automatic execution]
How the array is stored in memory:
Array sorting:
Select sort: the inner loop ends once and the maximum value appears in the position of the header.
Bubble sort: Two adjacent elements are compared. If the condition is eligible for transposition.
Public classarraysortdemo{Private Static int[] arr = {1, 3, 2, 4, 13, 9, 6}; Public Static voidMain (string[] args) {//System.out.println ("Hello world!"); //Selectsort (arr);Bubblesort (arr); Arrayprint (arr); //Arrys.sort (arr); This function is used for sorting in Java development. } 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; } } } } Public Static voidBubblesort (int[] arr) { for(intx=0; x<arr.length-1; X + + ) { for(inty=0; y<arr.length-x-1; y++)//-x: Reduce the number of times to compare each time, 1: Avoid crossing the corner mark. { if(arr[y]>arr[y+1]) { inttemp =Arr[y]; Arr[y]= Arr[y+1]; Arr[y+1] =temp; } } } } Public Static voidArrayprint (int[] arry) { for(inti = 0; i < arry.length; i++) {System.out.print ("arry[" + i + "]=" + arry[i] + ","); } } /*Displacement Location*/ Public Static voidSwapint[] arr,intAintb) {inttemp =Arr[a]; Arr[a]=Arr[b]; ARR[B]=temp; } }
Finding operations on arrays
/*the find operation for the array. Exercise: Insert an element into an ordered array to ensure that the array is still in order. */classarraytest4{ Public Static voidMain (string[] args) {int[] arr = {2, 1, 3, 5, 4, 7, 2, 9}; intindex = GetIndex (arr,3); System.out.println ("Index=" +index); int[] arr2 = {2, 3, 5, 7, 8, 12, 15}; } /*Practice:*/ Public Static intGetindex_2 (int[] arr,intkey) { intmin, Max, mid; Min= 0; Max= Arr.length-1; Mid= (min + max)/2; while(min<=max) {Mid= (min + max) >>2; if(Key >Arr[mid]) min= Mid + 1; Else if(Key <Arr[mid]) Max= Mid-1; return-1; Else returnmid; } returnmid; } /*binary Find, improve efficiency, but must ensure that the array is ordered. */ Public Static intHalfsearch (int[] arr,intKey) { intmin, Max, mid; Min= 0; Max= Arr.length-1; Mid= (min + max)/2; while(Arr[mid]! =key) { if(Key >Arr[mid]) min= Mid + 1; Else if(Key <Arr[mid]) Max= Mid-1; if(Min >max) { return-1; } Mid= (min + max)/2; } returnmid; } /*binary the second way. */ Public Static intHalfsearch_2 (int[] arr,intkey) { intmin, Max, mid; Min= 0; Max= Arr.length-1; Mid= (min + max)/2; while(min<=max) {Mid= (min + max) >>2; if(Key >Arr[mid]) min= Mid + 1; Else if(Key <Arr[mid]) Max= Mid-1; return-1; Else returnmid; } return-1; } //defines a function that gets the position of the first occurrence of the key value in the array, or 1 if it is returned, which means that the key does not exist in the array. Public Static intGetIndex (int[] arr,intkey) { for(intx = 0; x<arr.length; X + + ) { if(arr[x]==key) { returnx; } } return-1; } }
Binary conversions:
classarraytest5{ Public Static voidMain (string[] args) {toBin (6); } /*decimal --hexadecimal*/ Public Static voidTohex (intnum) {StringBuffer SB=NewStringBuffer (); for(intx = 0; x<8; X + +) { inttemp = num & 15; if(Temp > 9) { //System.out.println ((char) (temp-10+ ' A '));Sb.append ((Char) (temp-10+ ' A ')); } Else { //System.out.println (temp);sb.append (temp); } num= num >>> 4; } System.out.println (Sb.reverse ()); } /*Decimal Turn binary*/ Public Static voidToBin (intnum) {StringBuffer SB=NewStringBuffer (); while(num>0) {sb.append (num%); Num= NUM/2; } System.out.println (Sb.reverse ()); } }
/*Look up the table method: All elements are temporarily stored. Establish a correspondence relationship. */classarraytest6{ Public Static voidMain (string[] args) {Tohex (60); ToBin (6); } Public Static voidTohex (intnum) { Char[] chs = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ' }; //to define a temporary container Char[] arr =New Char[8]; intpos =arr.length; while(num!=0) { inttemp = num & 15; arr[--pos] =Chs[temp]; Num= num >>> 4; } for(intx = pos; x<arr.length; X + +) {System.out.print (arr[x]+","); } } Public Static voidToBin (intnum) { Char[] chs = {' 0 ', ' 1 '}; Char[] arr =New Char[32]; //defines a pointer to an array of operations intpos =arr.length; while(num! = 0) { inttemp = num & 1; arr[--pos] =Chs[temp]; Num= num >>> 1; } for(intx = pos; x<arr.length; X + +) {System.out.print (arr[x]); } }}
Two-dimensional arrays
Java Learning--arrays