An array is a data structure used to store a collection of values of the same type
1. The array needs to declare the array type (that is, the stored data type), such as: int [] A; where int is an array type [] represents a one-dimensional array, a represents the array name.
2. Create an array a:int [] a = new int[100]; Indicates that creating an array that stores 100 integers does not have a specific value when the default is 0.
You can also create an array of specific content directly: int [] A = {1, 2, 3, 4, 5, 6, 7, 8, 9};
the elements of the Boolean array are initialized to false;
The elements of an object array are initialized to a special value of NULL, which means that the elements have not yet held any objects. (For example: for (int i=0;i<100;i++) names[i]= "";
3. Multidimensional array: int [] [] a = new int[10][10]; Represents creating a two-dimensional array of 10*10, and so on.
Example: Using a two-dimensional array to generate a Yang Hui triangle:
1 PackageDemo1;2 ImportJava.util.Scanner;3 4 Public classDemo1 {5 Public Static voidMain (string[] args) {6SYSTEM.OUT.PRINTLN ("Please lose Yang Hui triangle layer");7 //8 9 Row is the get input data input statement8Scanner sc =NewScanner (system.in);9 introw =sc.nextint ();Ten sc.close (); One int[] m =New int[Row][row]; A for(inti = 0; i < row; i++) { - for(intk = 0; K < 8-i; k++) { - theSystem.out.print (""); -}//The body of this for statement is the number of spaces to print per line - - for(intj = 0; J < i + 1; J + +) { + if(j = = 0 | | i = =j) { -M[I][J] = 1;//determine the position of the Yang Hui triangle number 1 +}Else { AM[I][J] = M[i-1][j] + m[i-1][j-1]; at //find array-specific data contact - } -System.out.printf ("%2d", M[i][j]); - } -System.out.println ();//Blank Line - } in}
Bubble Sort Method:
PackageDemo1;Importjava.util.Arrays; Public classTest1 { Public Static voidMain (string[] args) {int[] arr = {5, 8, 1, 7, 4, 9, 6, 3, 2}; BooleanFlag =false;//total number of bubbles-1 non-mandatory for(inti = 0;! Flag && i < arr.length-1; i++) {flag=true; for(intj = 0; J < arr.length-1-I; J + +) { //-1 must prevent cross-border reduction I to improve efficiency if(Arr[j] > arr[j + 1]) { inttemp =Arr[j]; ARR[J]= Arr[j + 1]; Arr[j+ 1] =temp; //Determine unorderedFlag =false; }}} System.out.println (Arrays.tostring (arr)); }}
Select the sorting method:
1 int[] arr = {5, 8, 1, 7, 4, 9, 6, 3, 2};2 for(inti = 0; i < arr.length-1; i++) {3 //Assuming that the current I address is the location of the sample minimum value4 intnum =i;5 for(intj = i + 1; J < arr.length;j++) {6 //if the J position element is smaller than the current position element, update the minimum position7 if(Arr[num] >Arr[j]) {8num =J;9 }Ten } One //swaps the current minimum position element with the first element of the unordered area A inttemp =Arr[i]; -Arr[i] =Arr[num]; -Arr[num] =temp; the } -System.out.println (arrays.tostring (arr));
Insert Sort Method:
1 for(inti = 1; i < arr.length; i++) {2 //J Current position to be inserted forward3 intj =i;4 //Save the element to be inserted forward in temp.5 inttemp =Arr[j];6 //temp is less than the previous element of the current position7 while(J > 0 && Temp < arr[j-1]) {//use short-circuit and skip over-bounds8 //Save the previous element to the current location9ARR[J] = arr[j-1];Ten //move forward at current position Onej--; A } - //put the required data into the correct position -ARR[J] =temp; the - } -System.out.println (arrays.tostring (arr));
Java base Array (with base sort method)