0

stuck on something that should be pretty simple.

I have the class TreeSort:

public class TreeSort {

    public static <E extends Comparable<? super E>> void sort(E[] nums) {
        //Sorting
    }
}

And a simple Tester class with a main method for testing:

public class Tester {
    public static void main(String[] args) throws TreeStructureException {

        int[] nums = { 11, 2, 8, 30, 12, 21, 6, 4, 3, 18 };
        TreeSort.sort(nums); // The method sort(E[]) in the type TreeSort is not 
                             // applicable for the arguments (int[])
    }

}

Why do I get this error? Thanks all

1
  • Because an int[] isn't an Integer[]. Commented May 2, 2014 at 17:04

2 Answers 2

3

int[] nums is an Object. You have two ways to solve this:

  • Change the variable to Integer[].

  • Create an additional method to support int[].

In case you're not doing this for homework/exercise/specific sort algorithm purpose, use Arrays#sort(int[]) or Arrays#sort(Object[] array) instead.

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

1 Comment

Thanks, that was the ticket. Needed to use Integer[]
2

Primitives and generics aren't really compatible. You'll either need a Integer[] (gross) or sort should take a int[].

3 Comments

What's gross about Integer[]?
@NWard The memory overhead from boxing, loss of memory locality when traversing the array, and an extra pointer dereference for each element accessed mainly.
You're correct, needed to change int[] to Integer[]

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.