English 中文(简体)
通用方法——不能适用于特定类型
原标题:Generic method - cannot be applied to given types

我正致力于我的第一个方案,利用一种通用的方法。 我认为,我之所以这样做是正确的,办法是将参数定为<条码>。

public class SelectionSort {
protected int[] arrayOne = {1,2,3,4,5,6,7,8};
protected double[] arrayTwo = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0};
public static <T extends Comparable<T>> void selectionSort(T[] a)
{
for (int index =0; index < a.length; index++)
{
    int minElementIndex = index;
    T minElementValue = a[index];
    for (int i = index + 1; i < a.length; i++) 
    {
        if (a[i].compareTo(minElementValue) < 0)
        {
            minElementIndex = i;
            minElementValue = a[i];
        }
    }//end of inner for loop
    a[minElementIndex] = a[index];
    a[index] = minElementValue;
}//end of outer for loop
for(int indexb = 0; indexb<a.length; indexb++)
{
    System.out.printf("%d ", a[indexb]);
    if(indexb == a.length)
        System.out.println("");
}
}
public static void main(String[] args)
{
selectionSort(arrayOne);
selectionSort(arrayTwo);

}}//end of main and SelectionSort

也许你们能够帮助我。 如果是的话,我非常赞赏。

最佳回答

不能使用诸如<代码>int或double<>/code>等原始类型,这些类型不是物体或类别。 页: 1

尤其是,如果你想要支持接受原始<代码>int[]double[ 阵列,你将不得不两次书写该代码。 (我可以设想一种半工作,但不使用外部图书馆......)

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签