类型不适用于自变量(泛型问题)

时间:2018-07-17 23:27:18

标签: java generics

为什么我不能在代码中调用泛型方法?编译器说该类型不适用。这是我的Java代码。我是Java新手。如果这是一个明显的问题,我深表歉意。请帮忙。

package adnan1;
import java.util.Scanner;
public class myclass{
    public static void main(String args[]) {
        int length;
        myclass my = new myclass();
        Scanner input = new Scanner(System.in);
        System.out.println("ENTER LENGTH OF ARRAY");
        length = input.nextInt();
        int[] data1 = new int[length];
        int[] data2 = new int[length];
        int[] data3 = new int[length];
        System.out.printf("ENTER %d ELEMENTS FOR 1ST ARRAY\n",length);
        for(int i=0;i<length;i++) {
            data1[i] = input.nextInt();
        }
        System.out.printf("ENTER %d ELEMENTS FOR 2ND ARRAY\n",length);
        for(int j=0;j<length;j++) {
            data2[j] = input.nextInt();
        }
        System.out.printf("ENTER %d ELEMENTS FOR 3RD ARRAY\n",length);
        for(int k=0;k<length;k++) {
            data3[k] = input.nextInt();
        }
        my.bubblesort(data1, length);  //ERROR TYPE t[] not applicable for 
                                        //argument int[]. 
        my.selectionsort(data2, length); //ERROR TYPE t[] not applicable for 
                                        //argument int[].
        my.insertionsort(data3, length); //ERROR TYPE t[] not applicable for 
                                        //argument int[].
        input.close();
    }
    public static <t extends Comparable<t>> void bubblesort(t[] data,int length) {
        t temp = null;
        System.out.println("bubble sort");
        for(int i = 0 ; i<length-1 ; i++) {
            for(int j=0; j<length-1 ; j++) {
                if(data[j].compareTo(data[j+1])>0) {
                    temp = data[j];
                    data[j] = data[j+1];
                    data[j+1] = temp;
                }
            }
        }
        for(t z : data) {
            System.out.println(z);
        }       
    }
    public static<t extends Comparable<t>> void selectionsort(t[] data , int length) {
        int min;
        t temp = null;
        System.out.println("selection sort");
        for(int i=0;i<length;i++) {
            min = i;
            for(int j=i+1;j<length;j++) {
                if(data[j].compareTo(data[min]) > 0) {
                    min=j;
                }
            }
            temp = data[i];
            data[i] = data[min];
            data[min]  = temp;
        }
        for(t z : data) {
            System.out.println(z);
        }
    }
    public static <t extends Comparable <t>> void insertionsort(t[] data , int length) {
        int hole;
        t value;
        System.out.println("insertion sort");
        for(int i=1;i<length;i++) {
            hole = i-1;
            value = data[i];
            while(hole > -1 && data[hole].compareTo(value) > 0) {
                data[hole+1] = data[hole];
                hole--;
            }
            data[hole+1] = value;
        }
        for(t z : data) {
            System.out.println(z);
        }
    }
}

我的方法还可以,但是我不知道如何调用它们。总的来说,我说错了。请告诉我如何调用这些方法,以免错误不再发生?

0 个答案:

没有答案