Concept
This class contains various methods for manipulating arrays, such as sorting and searching. It is important to note that if the specified array reference is null, access to methods in this class throws a null pointer exception NullPointerException.
Common Methods
Sort method for sorting elements in the specified array ( element values are sorted from small to large )
The source arr array element {1,5,9,3,7}, sorted after the arr array element is {1,3,5,7,9}
Int[] arr = {1,5,9,3,7};
Arrays.sort (arr);
tostring Method that returns the string form of the contents of the specified array element
Int[] arr = {1,5,9,3,7};
String str = arrays.tostring (arr); the value of STR is [1, 3, 5, 7, 9]
The binarysearch method, in the specified array, to find where the value of the given element appears. If no query is reached, the return position is-1. Requires that the array must be an ordered array .
Int[] arr = {1,3,4,5,6};
int index = Arrays.binarysearch (arr, 4); The value of index is 2
int index2= Arrasy.binarysearch (arr, 2); The value of Index2 is-1
methods of arrays class practice
Defines a method that receives an array that stores 10 student test scores, which require the return of the last three test scores with the lowest test score.
public static int[] method (double[] arr) {
Arrays.sort (arr); Sorting array elements (element values are sorted from small to large)
Int[] result = new INT[3]; Three test scores after storage
system.arraycopy (arr, 0, result, 0, 3);//Copy the first 3 elements of the ARR array into the result array
return result;
}
Java->arrays class