-1

This is my code:

public void p10(int a, int b, int c){
    int[] nums = new int[] {a, b, c};
    Arrays.sort(nums, Collections.reverseOrder());
    System.out.println(Arrays.toString(nums));
}

What could be the problem that it doesn't work and it gives an error? Error states that:

java: no suitable method found for sort(int[],java.util.Comparator<java.lang.Object>) method java.util.Arrays.sort(T[],java.util.Comparator<? super T>) is not applicable (inference variable T has incompatible bounds equality constraints: int upper bounds: java.lang.Object) method java.util.Arrays.sort(T[],int,int,java.util.Comparator<? super T>) is not applicable (cannot infer type-variable(s) T (actual and formal argument lists differ in length))

1 Answers1

1

int[] is a primitive array that doesn't work with comparators, so you will need to convert it to an array of Integers before doing the sorting:

public void p10(int a, int b, int c){
    Integer[] nums = new Integer[] {a, b, c};
    Arrays.sort(nums, Collections.reverseOrder());
    System.out.println(Arrays.toString(nums));
}

Integer is just a wrapper object around int, which the java compiler can seamlessly inline with other math operations. Using an Integer is functionally (almost) the same as using a raw int.

0x150
  • 589
  • 2
  • 11