To define a method for an array:
int[] arr = new int [5]; ← New array defines the length of the array to be written in parentheses
Int[] arr1 ={1,2,3,4,5}; ← The contents of the defined array are directly written in curly braces
To assign a value to an array:
ARR[2] = 3; ← Directly write the index value in the array to give him the number of values on the line.
string[] STRs = new String [5]; ← A newly defined array of string types, if you do not fill in the contents, after the traversal output, each item inside is null. Returns 5 null after traversing the output.
Array sorting:
Int[] arr = {45, 34, 53, 43};
Arrays.sort (arr); ← Quick sort of array
System.out.println (arrays.tostring (arr)); ← Methods for outputting arrays in tool classes
Binary search method: (before using the binary search method, first to sort the array, using the. Sort method)
The binary search method in Java is called the binary lookup method, the array is arranged first, and then the arranged array is split into two parts from the middle position. Each time the value of the middle position is compared with the unknown Origin object, if the value of the middle position is larger than the unknown origin object, the first half of the lookup process is recycled, and if the median value is smaller than the unknown Origin keyword, the lookup process is cycled in the second half. There is no unknown origin keyword in the sequence until it is found.
int i = Arrays.binarysearch (arr, 34); ← Two parameters inside the parentheses, the left is the query from that array, the right is the query number, not find the words to return a negative.
System.out.println (i)
Binary search method range query a number:
Int J = Arrays.binarysearch (arr, 1, 3, 34); ← parameters are, which array, from index value 1, to index value 3 (not including 3, only to its previous index)
System.out.println (j);
Copying an array
. copyof Usage:
Int [] newArr = arrays.copyof (arr, 7); ← The left side of the argument is the original array, the right is the length of the new array (if the copied array is longer than the original, more than the 0 represented)
System.out.println (arrays.tostring (NEWARR));
. Copyofrange Usage:
int[] newArr1 = Arrays.copyofrange (arr, 1, 3); ← 3 parameters, the first is which array, the last two parameters are the index values in the array, from where to, not including the final number of the index value (the last number minus 1).
System.out.println (arrays.tostring (NEWARR1));
An array of Java