Arrays.sort(Object[], int, int): This method is available in java.util.Arrays class of Java.
Syntax:
void java.util.Arrays.sort(Object[] a, int fromIndex, int toIndex)
This method takes three arguments one of type Object array and the other two are of type int as its parameters. This method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
Parameters: Three parameters are required for this method.
a: the array to be sorted.
fromIndex: the index of the first element (inclusive) to be sorted.
toIndex: the index of the last element (exclusive) to be sorted.
Throws:
1. IllegalArgumentException - if fromIndex > toIndex.
2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length.
3. ClassCastException - if the array contains elements that are not mutually comparable.
Approach 1: When no exceptions
Java
import java.util.Arrays;public class ArrayssortObjectrange {public static void main(String[] args) {Object a[] = { 12, 6, 6, 2, 4, 56, 23 };int fromIndex = 0, toIndex = 3;Arrays.sort(a, fromIndex, toIndex);System.out.println(Arrays.toString(a));}}
Output:
[6, 6, 12, 2, 4, 56, 23]
Approach 2: IllegalArgumentException
Java
import java.util.Arrays;public class ArrayssortObjectrange {public static void main(String[] args) {Object a[] = { 12, 6, 6, 2, 4, 56, 23 };int fromIndex = 4, toIndex = 3;Arrays.sort(a, fromIndex, toIndex);System.out.println(Arrays.toString(a));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(4) > toIndex(3) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.sort(Arrays.java:1102)
Approach 3: ArrayIndexOutOfBoundsException
Java
import java.util.Arrays;public class ArrayssortObjectrange {public static void main(String[] args) {Object a[] = { 12, 6, 6, 2, 4, 56, 23 };int fromIndex = -1, toIndex = 3;Arrays.sort(a, fromIndex, toIndex);System.out.println(Arrays.toString(a));}}
Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.sort(Arrays.java:1102)
Approach 4: ClassCastException
Java
import java.util.Arrays;public class ArrayssortObjectrange {public static void main(String[] args) {Object a[] = { 12, 6, 'a', 2, 4, 56, 23 };int fromIndex = 0, toIndex = 3;Arrays.sort(a, fromIndex, toIndex);System.out.println(Arrays.toString(a));}}
Output:
Exception in thread "main" java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Character (java.lang.Integer and java.lang.Character are in module java.base of loader 'bootstrap') at java.base/java.lang.Character.compareTo(Character.java:132) at java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:321) at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188) at java.base/java.util.Arrays.sort(Arrays.java:1106)
No comments:
Post a Comment