An array is a data structure used to store a collection of values of the same type.
Arrays as objects allow memory allocation using the new keyword, and before using an array, you must first define the type to which the array variable belongs.
An array can access every value in an array by an integer subscript, for example, if an array of type A is present. The a[i] is the integer labeled I in the array (subscript starting from 0).
1. Declaration and initialization of arrays
1 //Statement2 int[] Iarr;3 String SARR [];4 5 //declaring and assigning sizes6 int[] iArr2 =New int[3];//the default value in the array is 07String [] sArr2 =NewSTRING[10];//array default value is null8System.out.println (arrays.tostring (IARR2));//Print Array9 System.out.println (arrays.tostring (SARR2));Ten /*Output One * [0, 0, 0] A [NULL, NULL , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL] - */ - the //declaring and assigning values - int[] IArr3 =New int[]{2,3,4,6}; -String [] SArr3 = {"One", "one", "one", "three"}; -System.out.println (arrays.tostring (IARR3));//[2, 3, 4, 6] +System.out.println (arrays.tostring (SARR3));//[One, both, three] - + //Simple to use AIarr =New int[]{1,2,3}; atSARR =NewString[2]; -Sarr[0] = "Hello"; -Sarr[1] = "Friend"; -System.out.println (iarr[1]);//2 -System.out.println (arrays.tostring (SARR));//[Hello, friend]
2. Operation of arrays
(1) Array traversal
// Traversal of an array int [] arr = {2,4,5,9,7}; for (int i = 0; i < arr.length; i++) { System.out.print (Arr[i]+ "\ t"); 2 4 5 9 7 } System.out.println (); for (int num:arr) { System.out.print (num+ "\ t"); 2 4 5 9 7 }
(2) Padding of arrays
The Arrays.fill () method can be populated with arrays, and the Fill () method has many overloaded methods that satisfy the padding or substitution of arbitrary array elements.
int New int [5]; // Fill Arrays.fill (arr, 6); System.out.println (arrays.tostring (arr)); // [6, 6, 6, 6, 6] // Replace Arrays.fill (arr,2,4,0); System.out.println (arrays.tostring (arr)); // [6, 6, 0, 0, 6]
(3) Sorting of arrays
Arrays.sort () method to sort the array
int [] arr = {5, 4, 3, 2, 1}; Arrays.sort (arr); System.out.println (arrays.tostring (arr)); // [1, 2, 3, 4, 5]
(4) Copying of arrays
The copyof () method and the Copyofrange () method in the arrays class enable the copying of arrays
The former is the copy array to the specified length, which copies the specified length array into a new array (or an array before overwriting).
COYPOF (arr, int newlength) can be used for expansion of arrays
-arr: The array to be copied
-newlength: The length of the array after copying, plus 0 or null if the new array is copied longer than arr. If the new array is smaller than the length of arr, 0 of arr is copied to start until the length of the array is satisfied.
int New int []{3,4,5= arrays.copyof (arr, arr.length*2); System.out.println (arrays.tostring (arr)); // [3, 4, 5, 0, 0, 0] int [] NewArr = arrays.copyof (arr, 2); System.out.println (arrays.tostring (NEWARR)); // [3, 4]
Copyofrange (arr, int fromIndex, int toindex)
-arr: the array to copy
-fromindex: Specifies the starting index position of the array to copy, not greater than the length of arr
-toindex: Specifies that the last index position of the array to be copied (excluding Toindex) can be greater than the length of arr
int [] arr = {5, 7, 8, 2, 9}; = Arrays.copyofrange (arr, 0, arr.length*2); System.out.println (arrays.tostring (arr)); // [5, 7, 8, 2, 9, 0, 0, 0, 0, 0] int [] NewArr = Arrays.copyofrange (arr, 1, 3); System.out.println (arrays.tostring (NEWARR)); // [7, 8]
The Arraycopy method in the System class copy the elements of the array to another array by:
System.arraycopy (SRC, Srcpos, dest, Destpos, length)
-SRC: the array to copy
-srcpos: Index position of the array to be copied
-dest: Target Array
-destpos: Index position of the target array
-length: The length to be copied, counting from the index position
int [] arr = {6, 4, one, one}; int New int []{5,4,9,3,4,22, NEWARR, 0, 2);
System.out.println (arrays.tostring (NEWARR)); // [One, 9, 3, 4, 2]
3. Sorting of arrays
(1) Bubble sort
The basic idea of bubbling is to compare the values of adjacent elements, Exchange element values if conditions are met, move smaller elements to the front of the array, and move large elements behind the array.
//Bubble Sort int[] arr =New int[]{12, 3, 65, 45, 88, 1, 7};
for(inti = 0; i < arr.length-1; i++){ for(intj = 0; J < arr.length-1-I; J + +){ if(Arr[j] > arr[j+1]){ inttemp =Arr[j]; ARR[J]= Arr[j+1]; Arr[j+1] =temp; }}} System.out.println (Arrays.tostring (arr));//[1, 3, 7, .
(2) Direct Select sort
The idea of direct selection is to compare the specified sort position with the other array elements, and if the condition is met, the element is swapped, and the maximum number is exchanged with the "last" element.
//Direct Select Sort int[] arr =New int[]{12, 3, 65, 45, 88, 1, 7}; intindex; for(inti = 1; i < arr.length; i++) {Index=0; //find the maximum value for(intj = 0; J <= Arr.length-i; J + +){ if(Arr[j] >Arr[index]) {Index=J; } } //The maximum value is placed in the last position inttemp =Arr[index]; Arr[index]= Arr[arr.length-i]; Arr[arr.length-I] =temp; } System.out.println (Arrays.tostring (arr));//[1, 3, 7, .
(3) Reverse sort
Reverses the elements of the array, swaps the last element of the element with the first element, and swaps the second element to the second of the array ....
// Flip Sort-the position of the array is reversed int New int []{1, 2, 3, 4, 5, 6, 7}; int arrlength = arr.length; for (int i = 0; i < ARRLENGTH/2; i++ ) { int temp = arr[i]; = arr[arrlength-1-i]; Arr[arrlength-1-i] = temp; } System.out.println (arrays.tostring (arr)); // [7, 6, 5, 4, 3, 2, 1]
Java Array Operations