3

I've just started programming around 2 weeks ago so please don't be too strict :)

I tried to solve a programming exercise to print the 3 largest elememts of an Array but the .sort Method

reports an error and I don't know why. It seems like I have declared my Array in the wrong way but I can't spot the mistake.

public static void main(String[] args) {
    int [] elements = {1, 4, 17, 7, 25, 3, 100};
    int k = 3;
    System.out.println("Original Array: ");
    System.out.println(Arrays.toString(elements));
    System.out.println(k +" largest elements of the said array are:");
    Arrays.sort(elements, Collections.reverseOrder());         
   for (int i = 0; i < k; i++) 
      System.out.print(elements[i] + " ");
}

}

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
Encera
  • 45
  • 5

5 Answers5

5

Because the integers are unboxed and are not objects. They need to be boxed, i.e Integer.

Unboxed

int [] elements = {1, 4, 17, 7, 25, 3, 100};

Boxed

Integer [] elements = {1, 4, 17, 7, 25, 3, 100};

Using Java 8 Streams

       int[] elements = { 1, 4, 17, 7, 25, 3, 100 };

       int[] sortedUnboxed = IntStream.of(elements)
                .boxed()
                .sorted(Comparator.reverseOrder())
                .mapToInt(value -> value)
                .toArray();

        Integer[] sortedBoxed = IntStream.of(elements)
                .boxed()
                .sorted(Comparator.reverseOrder())
                .toArray(Integer[]::new);
Jason
  • 4,590
  • 2
  • 9
  • 18
4

int[] as Collections.reverseOrder doesn't work for primitive types. Please use Integer instead. It will solve the issue.

Piyush Maheswari
  • 600
  • 2
  • 15
1

Look at the definition of reverseOrder: https://www.javatpoint.com/java-collections-reverseorder-method

It will tell you that it looks like this:

public static <T> Comparator<T> reverseOrder()

the <T> is the generic template. Primitives cannot be used as generic types, see Why don't Java Generics support primitive types?

int is primitive type. Luckily Integer is a wrapper class around int and you can use it as generic. The rest is homework for the student.

I feel your pain that int cannot be a generic, but it is how it is.

Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148
1

The other answers addressed on how to reverse your primitive int array, but according to your problem question, it seems you do not need to reverse the array at all.

Simply iterate through your array beginning at the end by changing your for conditions:

public static void main(String[] args)
{
    int [] elements = {1, 4, 17, 7, 25, 3, 100};
    int k = 3;
    System.out.println("Original Array: ");
    System.out.println(Arrays.toString(elements));
    System.out.println(k +" largest elements of the said array are:");
    Arrays.sort(elements);         
    for (int i = elements.length - 1; i > elements.length - k; i--) 
      System.out.print(elements[i] + " ");
}

Output:

Original Array: 
[1, 4, 17, 7, 25, 3, 100]
3 largest elements of the said array are:
100 25 17 
Nexevis
  • 4,024
  • 3
  • 11
  • 20
1

If you wanted to stick with int, just sort it and print out the last three:

public static void main(String[] args) 
{
    int [] elements = {1, 4, 17, 7, 25, 3, 100};
    int k = 3;
    System.out.println("Original Array: ");
    System.out.println(Arrays.toString(elements));
    System.out.println(k +" largest elements of the said array are:");
    Arrays.sort(elements);         
    for (int i = 0; i < 3; i++) 
      System.out.print(elements[elements.length - 1 - i] + " ");
}
MrsNickalo
  • 207
  • 1
  • 6