Auswahl Sortieren mit Generika

Habe ich Auswahl Sortieren mit Ganzzahlen und es funktionierte, als ich versuchte, ändern Sie das Programm, um die Arbeit mit generischen Typen kann der compiler beschweren und ich weiß nicht, wie es zu lösen ist. Wenn jemand kann, zeigen einige Tipps und Konstruktive Kommentare wäre ich dankbar. Hier ist der 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] + ", ");
        }
    }
}

Das folgende ist, was javac spuckt.

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[])
  • Was ist der compiler, sagen Sie?
  • int erstreckt sich nicht Object
  • Die Botschaft lautet: Exception in thread "main" java.lang.Error: Unresolved compilation Problems: Die Methode printArray(E[]) in der Art SelelctionSort ist nicht anwendbar für die Argumente (int[]) Die Methode selectionSort(E[]) in der Art SelelctionSort ist nicht anwendbar für die Argumente (int[]) Die Methode printArray(E[]) in der Art SelelctionSort ist nicht anwendbar für die Argumente (int[]) bei SelelctionSort.main(SelelctionSort.java:8)
  • Ich habe versucht mit einem array-Objekt, aber der compiler beschwert sich mit: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Bound mismatch: Die generische Methode selectionSort(E[]) Typ SelelctionSort ist nicht anwendbar für die Argumente (Object[]). Der abgeleitete Typ-Objekt ist kein Gültiger Ersatz für die begrenzt den parameter <E extends Vergleichbar<E>>
  • versuchen Integer[] list = {34, 17, 23, 35, 45, 9, 1}; Die runtime-Fehlern sind, weil Eclipse ist schlecht und kompiliert den code, die ungültig ist. Mit javac wird nicht lassen Sie es kompilieren.
Schreibe einen Kommentar