Array
Concept: A collection of data of the same type, in fact an array is a container.
The benefits of arrays: You can automatically number the elements in the array starting with 0 to facilitate manipulation of these elements.
Format 1: element type [] Array name = new element type [number of elements];
Format 2: element type [] Array name = new element type []{element 1, Element 2, element 3,...., element n};
Partition of Memory:
1: Register
2: Local Method area
3: Method Area
4: Stack Memory
Stores all local variables.
And once the scope of the variable belongs to the end, the variable is automatically released
5: Heap Memory
Arrays and objects are stored (in fact arrays are objects) all new builds are in the heap.
Characteristics:
1. Each entity has an initial address value
2. Each variable in the heap memory has a default initialization value, which differs depending on the type.
Class Arraydemo{public static void Main (string[] args) {int[] arr = new INT[3]; System.out.println (arr (3)); arrayindexoutofboundsexception//This exception occurs when you access a corner label that does not exist in the array. arr = null; System.out.println (Arr[0]);//nullpointerexeception//when a reference variable does not have any entities pointing, it is also used to manipulate the entity, and the exception occurs.}
The most basic action of an array operation is to save and fetch.
The core idea: is the operation of the diagonal mark.
Common array Application algorithms
Array traversal
Int[] arr = {89,34,280,15};for (int x = 0; x<arr.length; x + +) {System.out.println (arr[x]);}
Array Maximum Value
public static int Getmax (int[] arr) {//define variable record large value int max = arr[0];for (int x=1; x<arr.length; x + +) {if (Arr[x]>max) {m AX = arr[x];}} return Max;}
The second method public static int Getmax (int[] arr) {//definition variable records a larger value int max = 0;for (int x=1; x<arr.length; x + +) {if (arr[x]>arr[ Max]) {max = x;}} return Arr[max]; This returns the maximum value of the corner mark}
Select sort
public static void Selectsort (int[] arr) {for (int x=0; x<arr.length; x + +) {for (int y=x+1; y<arr.length; y++) {if (Arr[x]>arr[y]) {int temp = arr[x];arr[x] = arr[y];arr[y] = temp;}}}
Bubble sort
/* Inner Loop:-1: In order to avoid cross-border x: In order to increase the outer loop one time, the inner loop parameter and the number of elements of the comparison are decremented. */for (int x=0; x<arr.length; x + +) {for (int y=0, y<arr.length-1- x; y++) {if (arr[y]>arr[y+1]) {int temp = Arr[y];arr[y] = arr[y+1];arr[y+1] = temp;}}} Method two: for (int x=arr.length-1; x>0; x--) {for (int y=0; y<x; y++) {if (arr[y]>arr[y+1]) {int temp = arr[y];arr[ Y] = arr[y+1];arr[y+1] = temp;}}}
Lookup : Returns the corner label of an array
public static GetIndex (int[] arr,int value) {for (int x=0; x<arr.length; x + +) {if (arr[x] = = key) {return x;}} return-1;}
Binary Conversion 10->16
public static void Tohex (int num) {for (int x=0; x<8; x + +) {int temp = num & 15;if (temp>9) System.out.print ((Cha R) (temp-10+ ' A ')); ElseSystem.out.print (temp); num = num >>> 4;}}
When do you use arrays?
If the data has a corresponding relationship, and the party of the corresponding relationship is an ordered numeric number and used as a corner label. At this point you have to think about the use of arrays.
The data can be stored in an array, and the corresponding elements in the array are directly checked according to the result of the operation as a corner tag.
This way: called the look-up table method.
public static void tohex_1 (int num) {//define a corresponding relational table char[] CHS = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};for (int x=0; x<8; x + +) {int temp = num & 15; System.out.println (chs[temp]); num = num >>> 4;}} But this method is shown to have been written in the opposite public static void tohex_2 (int num) {if (num = = 0) {System.out.println ("0"); return;} Define a corresponding relational table char[] CHS = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};/* will look up more data data than one, It is stored and operated. So define an array---temporary container. */char[] = arr = new Char[8];int pos = arr.length;while (num! = 0) {int temp = num & 15;arr [--pos] = Chs[temp];num = num >>> 4;} System.out.println ("pos=" + pos); for (int x = pos; x<arr.length; x + +) {System.out.println (Arr[x] + ",");}}
Format of a two-dimensional array definition
Class Array2demo{public static void Main (string[] args) {int[][] arr = new int[3][2];//This is the definition format of a two-dimensional array.//The array has 3 large boxes, There are 2 small boxes in each large box System.out.println (arr); [[email protected] @ left is the type of entity. @ Right is the hash value of the entity. System.out.println (arr);//Print two-dimensional array System.out.println (arr[0]) directly; Directly print a two-dimensional array in the corner label 0 below the one-dimensional array/*********************************************************************/int[][] arr = new int[ 3][];arr[0] = new Int[2];arr[1] = new Int[1];arr[2] = new int[3];//Initializes each of the decimal groups in a two-dimensional array, respectively./****************************** /int[][] arr = new int[3][2]; System.out.println (arr.length); Printing the length of a two-dimensional array is actually the number of one-dimensional arrays System.out.println (arr[1].length);/**************************************************** The second way of defining int[][] arr = {{8,3,6},{7,4,9},{3,0,7}};int sum = 0;//traverse the two-dimensional array for (int x=0; x<arr.length; X + +) {for (int y=0; y<arr[x].length; y++) {System.out.println (arr[x][y]+ ","); sum + = Arr[x][y];}} System.out.println ("sum=" +sum);}
An array of Java learning personal memos