selection sort with generics

别来无恙 提交于 2019-12-01 12:29:40

问题


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

回答1:


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




回答2:


As said above you are using selectionSort(E[]) where E extends Comparable means , your selection sort can take the argument who implemented the Comparable interface. As int is primitive data so it's giving the compilation Error. So if you want generic feature then you can use Wrapper classes, All wrapper classes implemented the Comparable interface. The below code will work, Just edit version of your code

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



回答3:


it is really working this solution is working with all types of variables and as i m passing the String variable it is showing error



来源:https://stackoverflow.com/questions/12128066/selection-sort-with-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!