Domanda

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

È stato utile?

Soluzione

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.

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top