选择排序与泛型

时间:2012-08-26 06:23:04

标签: java generics primitive

我选择整数排序并且它正常工作,当我尝试修改程序以使用泛型时,编译器正在抱怨并且我不知道如何解决它。如果有人能指出一些提示和建设性意见,我将不胜感激。这是代码。

public class SelelctionSort 
{
    public static void main(String[] args) 
    {
        int[] list = {34, 17, 23, 35, 45, 9, 1};
        System.out.println("Original Array: ");
        printArray(list);

        selectionSort(list);
        System.out.println("\nSelection sort:");
        printArray(list);

    }

    //selection sort
    public static <E extends Comparable<E>> void selectionSort(E[] list)
    {
        for(int i=0; i<list.length -1; i++)
        {
            int iSmallest = i;

            for(int j=i+1; j<list.length; j++)
            {
                if(list[iSmallest].compareTo((list[j])) > 0  )
                {
                    iSmallest = j;
                }
            }
            E iSwap = list[iSmallest];
            list[iSmallest] = list[i];
            list[i] = iSwap;

        }
    }

    public static <E> void printArray(E[] list)
    {

        for(int i=0; i<list.length; i++)
        {
            System.out.print(list[i] + ", ");
        }
    }
}

以下是javac吐出的内容。

SelelctionSort.java:7: error: method printArray in class SelelctionSort cannot be applied to given types;
        printArray(list);
        ^
  required: E[]
  found: int[]
  reason: inferred type does not conform to declared bound(s)
    inferred: int
    bound(s): Object
  where E is a type-variable:
    E extends Object declared in method <E>printArray(E[])
SelelctionSort.java:9: error: method selectionSort in class SelelctionSort cannot be applied to given types;
        selectionSort(list);
        ^
  required: E[]
  found: int[]
  reason: inferred type does not conform to declared bound(s)
    inferred: int
    bound(s): Comparable<int>
  where E is a type-variable:
    E extends Comparable<E> declared in method <E>selectionSort(E[])
SelelctionSort.java:11: error: method printArray in class SelelctionSort cannot be applied to given types;
        printArray(list);
        ^
  required: E[]
  found: int[]
  reason: inferred type does not conform to declared bound(s)
    inferred: int
    bound(s): Object
  where E is a type-variable:
    E extends Object declared in method <E>printArray(E[])

3 个答案:

答案 0 :(得分:3)

int[] list = {34, 17, 23, 35, 45, 9, 1};
...
selectionSort(list);

您正在尝试致电selectionSort()哪个签名为selectionSort(E[]),但int未展开Comparable(它是primitive,甚至不是对象) - 因此类型不匹配。

您可以尝试创建Integer[]并传递它。 Integer是一个对象,它会扩展Comparable<Integer>
另一种方法是重载 selectionSort()接受对象的泛型类型并为每个所需的原语重载它。这是java用于Arrays.sort()方法的解决方案。

同样适用于printArray()

答案 1 :(得分:1)

如上所述,您正在使用selectionSort(E[]),其中E扩展了Comparable意味着,您的选择排序可以采用实现Comparable接口的参数。因为int是原始数据所以它给出了编译错误。因此,如果您需要通用功能,那么您可以使用Wrapper类,所有包装类都实现了Comparable接口。以下代码可以使用,只需编辑代码版本

public class SelelctionSort 
{
public static void main(String[] args) 
{
    Integer[] list = {34, 17, 23, 35, 45, 9, 1};
    System.out.println("Original Array: ");
    printArray(list);

    selectionSort(list);
    System.out.println("\nSelection sort:");
    printArray(list);
    Float[] flist = {34.4f, 17.6f, 23.0f};
    selectionSort(list);
}

//selection sort
public static <E extends Comparable<E>> void selectionSort(E[] list)
{
    for(int i=0; i<list.length -1; i++)
    {
        int iSmallest = i;

        for(int j=i+1; j<list.length; j++)
        {
            if(list[iSmallest].compareTo((list[j])) > 0  )
            {
                iSmallest = j;
            }
        }
        E iSwap = list[iSmallest];
        list[iSmallest] = list[i];
        list[i] = iSwap;

    }
}

public static <E> void printArray(E[] list)
{

    for(int i=0; i<list.length; i++)
    {
        System.out.print(list[i] + ", ");
    }
}
}

答案 2 :(得分:-1)

它真的有效 此解决方案适用于所有类型的变量 当我传递String变量时,它显示错误