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:

  1. If the target element found in sorted array then the result is the index of match
int[] rollNumbers={23,12,59};Arrays.sort(rollNumbers); // after sorting {12, 23, 59}
System.out.println(Arrays.binarySearch(rollNumbers, 23)); // found //at index 1

2. If the target element not found in sorted array then negative value showing one smaller than the negative of the index, where a match needs to be inserted to preserve sorted order.

int[] rollNumbers={23,12,59};Arrays.sort(rollNumbers); // after sorting {12, 23, 59}
System.out.println(Arrays.binarySearch(rollNumbers, 2)); // -1

Here it searches for the index of 2. Although 2 is not in the list, the search can determine that it should be inserted at element 0 to preserve the sorted order. Since 0 already means something for array indexes, Java needs to subtract 1 to give us the answer -1.

what is the output of the below code:

int[] rollNumbers={23,12,59};
Arrays.sort(rollNumbers); // after sorting {12, 23, 59}
System.out.println(Arrays.binarySearch(rollNumbers, 20)); // -2

Although 20 is not in the list, it would need to be inserted at element at 1 to preserve the sorted order. we negate and subtract 1 for consistency.

3. If the input array is unsorted array then the result is not predictable.

Comparing:

Arrays class has compare() method to compare two arrays to determine which is “smaller”. The return value of compare methods will be any of the following:

  1. Negative number means the first array is smaller than the second.
int[] array1={5};
int[] array2={10};
System.out.println(Arrays.compare(array1,array2));//prints negative number

2.A zero means the arrays are equal.

int[] array1={10};
int[] array2={10};
System.out.println(Arrays.compare(array1,array2));//prints zero

3.A positive number means the first array is larger than the second.

int[] array1={20};
int[] array2={15};
System.out.println(Arrays.compare(array1,array2));//prints positive number

now suppose if array lengths are different then the following rules will be applicable:

  1. If both arrays are same length and have the same values in each spot in the same order then it returns zero.
int[] array1={10,15};
int[] array2={10,15};
System.out.println(Arrays.compare(array1,array2));//prints zero

2.If all the elements are the same but the second array has extra elements at the end then it returns a negative number.

int[] array1={10,15};
int[] array2={10,15,20,25};
System.out.println(Arrays.compare(array1,array2));//prints negative number

3. If all the elements are the same but the first array has extra elements at the end then it returns a positive number.

int[] array1={10,15, 20,25};
int[] array2={10,15};
System.out.println(Arrays.compare(array1,array2));//prints positive number

mismatch:

If the arrays are equal, mismatch() returns -1. Otherwise, it returns the first index where they differ.

int[] array1={10,15};
int[] array2={10,15};
System.out.println(Arrays.mismatch(array1,array2));//prints -1
int[] array3={10,15,20};
int[] array4={10,15};
System.out.println(Arrays.mismatch(array3,array4));//prints index 2

Things to Remember:

java.uti.Arrays class provides the methods sort(), binarySearch(), compare() and mismatch() for the array operations.

Learning Science shows that learning a bit each day exponentially
improves your ability to retain your knowledge". --- Keep Learning.

 

Comments

Popular posts from this blog

How intern() method works in Java

About “var” Keyword In Java