Arrays class usage in Java
Java provides the “Arrays” class to perform sorting , searching and comparing arrays. Sorting: Let’s see simple example: int rollNumbers[] = {23,12,59}; Arrays.sort(rollNumbers); for(int i=0; i<rollNumbers.length; i++){ System.out.print(rollNumbers[i] + " "); } The result is 12,23,59 as expected. Guess the output of the below example: String rollNumbers[] = {"10","9","100"}; Arrays.sort(rollNumbers); for(String rollNumber: rollNumbers){ System.out.print(rollNumber + " "); } The result is 10, 100, 9. Here String sorts in alphabetical order, and 1 sorts before 9. Numbers sort before letters, and uppercase sorts before lowercase. Searching: Arrays class has binarySearch() method to search an element. The input parameters are sorted array and search element. The output is depend on the scenario: If the target element found in sorted array then the result is the index of match int[] rollNumbers={23,12,59}; Arrays.sort(rollNumbers