用泛型进行选择排序 [英] selection sort with generics

查看:178
本文介绍了用泛型进行选择排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用整数进行选择排序并且它正在工作,当我试图修改程序以使用泛型编译器抱怨并且我不知道如何修复它时。如果任何人都可以提出一些建议和建设性的意见,我将不胜感激。这是代码。

  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(\\\
Selection sort:);
printArray(list);

}

//选择类型
public static< E extends Comparable< E>> void selectionSort(E [] list)
{
for(int i = 0; i< list.length -1; i ++)
{
int iSmallest = i; (列表[iSmallest] .compareTo((列表[j]))(b)(b)(b) > 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 {
System.out.print (list [i] +,);
}
}
}

以下是 javac 吐出。


  SelelctionSort.java: 7:错误:类SelelctionSort中的方法printArray不能应用于给定的类型; 
printArray(list);
$
required:E []
found:int []
reason:推断的类型不符合声明的边界(b)
推断:int
bound(s):Object
其中E是一个类型变量:
E extends在方法< E> printArray(E [])中声明的对象
SelelctionSort.java:9:错误:方法selectionSort在类SelelctionSort不能应用于给定的类型;
selectionSort(list);
$
required:E []
found:int []
reason:推断的类型不符合声明的边界(b)
推断:int
bounds(s):Comparable< int>
其中E是一个类型变量:
E扩展了Comparable< E>在方法< E> selectionSort(E [])中
SelelctionSort.java:11:错误:类SelelctionSort中的方法printArray不能应用于给定类型;
printArray(list);
$
required:E []
found:int []
reason:推断的类型不符合声明的边界(b)
推断:int
bound(s):Object
其中E是一个类型变量:
E extends在方法< E> printArray(E [])中声明的对象

 


解决方案

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

您试图调用 selectionSort()签名是 selectionSort(E []),但 int 不扩展可比较(它是原语,甚至不是一个对象) - 因此类型不匹配。

你可以尝试创建一个 Integer [] 和交上来。 整数 是一个对象,它扩展了 Comparable< Integer>

替代方法是为重载 selectionSort()接受对象的泛型类型并为每个需要的原语。这是java为其 Arrays.sort() 方法。

c $ c> printArray()


I did selection sort with integers and it was working, when I tried to modify the program to work with generics the compiler is complaining and I don't know how to fix it. If anyone can point some tips and constructive comments I would be grateful. Here is the code.

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] + ", ");
        }
    }
}

The following is what javac spits out.

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);

You are trying to call selectionSort() which signature is selectionSort(E[]), but int does not extend Comparable (It is a primitive, not even an object) - and thus the types are not matching.

You can try to create an Integer[] and pass it. Integer is an object and it extends Comparable<Integer>.
The alternative is to overload selectionSort() to accept both generic type for objects and to overload it for each needed primitive. This is the solution java uses for its Arrays.sort() method.

The same holds for printArray()

这篇关于用泛型进行选择排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆