0

Could someone please kindly tell me why the code below wouldn't work unless the array is Integer[], not int[]

Integer[] one = {2,5,8,1,3,4,9};

Arrays.parallelSort(one, (p1, p2) -> p2 - p1);

System.out.println(Arrays.toString(one));

2 Answers 2

3

Because Arrays has no method parallelSort that takes an int[] and a Comparator.

The only parallelSort overload that takes a Comparator second argument is the one with a generic first argument (static <T> void parallelSort(T[] a, Comparator<? super T> cmp)), thus requiring that the first argument be an array of references. Integer[] satisfies that requirement, while int[] does not.

Sign up to request clarification or add additional context in comments.

1 Comment

Part of the issue here is that Java implements stuff like generics and templates at run time, which it why objects are needed and native types like int can't be used. Compare this to C++, where templates are implemented at compile time, eliminating these issues, and can be used with native types.
2

Because the signature of parallelSort is: public static <T extends Comparable<? super T>> void parallelSort(T[] a) where T is a class of obhects to be sorted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.