简体   繁体   English

Java使用通用类型

[英]Java Using Generic Type

I have a quicksort that takes in generic type T array I created an int array and try to use the quicksort but it doesn't know how to take in int array. 我有一个接受通用类型T数组的quicksort,我创建了一个int数组并尝试使用quicksort,但是它不知道如何接受int数组。 I cant call intArray = quickSort(intArray); 我不能打电话给intArray = quickSort(intArray); on the main method. 在主要方法上。 what can i do so i can use the generic type quicksort method? 我该怎么做才能使用通用类型的quicksort方法?

public class BasicTraining 
{
    public static <T extends Comparable<? super T>> T[] quickSort(T[] array) 
    {
        sort(0, array.length - 1, array);
        return array;
    }

    public static <T extends Comparable<? super T>> void sort(int low, int high, T[] array)
    {
        if (low >= high) return;
        int p = partition(low, high, array);
        sort(low, p, array);
        sort(p + 1, high, array);
    }

    private static <T extends Comparable<? super T>> int partition(int low, int high, T[] array)
    {
        T pivot = array[low];

        int i = low - 1;
        int j = high + 1;
        while (i < j)
        {
            i++; 
            while (array[i].compareTo(pivot) < 0) 
                i++;
            j--; 
            while (array[j].compareTo(pivot) > 0) 
                j--;
            if (i < j) 
                swap(i, j, array);
         }
         return j;
      }

    private static <T extends Comparable<? super T>> void swap(int i, int j, T[] array)
    {
        T temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void main(String[] args)
    {
        int[] intArray = new int[] {9,3,6,2,1,10,15,4,7,22,8};
        for(int i = 0; i < intArray.length; i++)
        {
            System.out.print(intArray[i] + ", ");
        }
//      intArray = quickSort(intArray);

    }
}

A generic type is a generic class or interface that is parameterized over types and java does not support primitive type. 泛型类型是通过类型进行参数化的泛型类或接口,而Java不支持原始类型。

Use Integer array rather than primitive type int. 使用Integer数组而不是基本类型int。

Integer[] intArray = new Integer[] {9,3,6,2,1,10,15,4,7,22,8};

You need array of Integer not primitives. 您需要Integer数组而不是原始数组。 From oracle docs for Generic Types 从oracle文档获取泛型类型

A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable. 类型变量可以是您指定的任何非原始类型 :任何类类型,任何接口类型,任何数组类型,甚至另一个类型变量。

Integer[] intArray = new Integer[]{9, 3, 6, 2, 1, 10, 15, 4, 7, 22, 8};

That's because int is a primitive type and thus is not of type T extends Comparable<? super T> 那是因为int是原始类型,因此不是T extends Comparable<? super T>类型T extends Comparable<? super T> T extends Comparable<? super T> . T extends Comparable<? super T>

Therefore you either need to convert your array into Integer[] or write a specific quicksort(int[] array) method. 因此,您需要将数组转换为Integer[]或编写特定的quicksort(int[] array)方法。 That is actually what is done in the JDK. 这实际上是在JDK中完成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM