java - 使用泛型进行选择排序

标签 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[])

最佳答案

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() 也是如此

关于java - 使用泛型进行选择排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128066/

相关文章:

java - java中使用原语进行字符串连接

java - 字节不是有符号的二进制补码吗?

c# - 为什么原始数据类型在不包含 System 命名空间的情况下也能工作?

java - Android 上随机访问写入 SAF 文件

java - 我应该将 Swing 组件声明为实例变量,还是只是从函数返回它们?

Java 通用比较器

spring - Resttemplate无法解析类型T的数组

java - NullPointerException - edittext.gettext()

java - Hibernate3.5 是否支持 GlassfishV2 上的 JPA1+EJB3.0?

java - getClass()方法的返回类型描述中的 "static type of expression"是什么意思?