Java 泛型数据类型参数

Java 泛型数据类型参数,java,arrays,generics,parameters,Java,Arrays,Generics,Parameters,我有一个通过selectionSort处理排序数组的类。我在主函数中排序数组时遇到问题(附在底部)…调用selectionsort()的正确方法是什么 我的问题:“类型SortArray中的方法selectionSort(T[],int)不适用于参数(int[],int)”。。。我正试图将我的int数组传递给函数,函数一直给我这个错误 /** Class for sorting an array of Comparable objects from smallest to larg

我有一个通过
selectionSort
处理排序数组的类。我在主函数中排序数组时遇到问题(附在底部)…调用
selectionsort()
的正确方法是什么

我的问题:“类型
SortArray
中的方法
selectionSort(T[],int)
不适用于参数
(int[],int)
”。。。我正试图将我的int数组传递给函数,函数一直给我这个错误

/**
   Class for sorting an array of Comparable objects from smallest to 
   largest.
*/
public class SortArray
{
   /** Sorts the first n objects in an array into ascending order.
       @param a  an array of Comparable objects
       @param n  an integer > 0 */
   public static <T extends Comparable<? super T>> void selectionSort(T[] a, int n)
   {
      for (int index = 0; index < n - 1; index++)
      {
         int indexOfNextSmallest = getIndexOfSmallest(a, index, n - 1);
         swap(a, index, indexOfNextSmallest);
         // Assertion: a[0] <= a[1] <= . . . <= a[index] <= all other a[i]
      } // end for
   } // end selectionSort

   /** Finds the index of the smallest value in a portion of an array.
       @param a      an array of Comparable objects
       @param first  an integer >= 0 and < a.length that is the index of 
                     the first array entry to consider
       @param last   an integer >= first and < a.length that is the index 
                     of the last array entry to consider
       @return the index of the smallest value among
               a[first], a[first + 1], . . . , a[last] */
   private static <T extends Comparable<? super T>>
           int getIndexOfSmallest(T[] a, int first, int last)
   {
      T min = a[first];
      int indexOfMin = first;
      for (int index = first + 1; index <= last; index++)
      {
         if (a[index].compareTo(min) < 0)
         {
            min = a[index];
            indexOfMin = index;
         } // end if
         // Assertion: min is the smallest of a[first] through a[index].
      } // end for

      return indexOfMin;
   } // end getIndexOfSmallest

   /** Swaps the array entries a[i] and a[j].
       @param a  an array of objects
       @param i  an integer >= 0 and < a.length
       @param j  an integer >= 0 and < a.length */
   private static void swap(Object[] a, int i, int j)
   {
      Object temp = a[i];
      a[i] = a[j];
      a[j] = temp; 
   } // end swap
} // end SortArray


public static void main(String[] args) {
    int[] anArray = {15, 8, 10, 2, 5};          // given array

    System.out.println("Printing unsorted array...");
    for(int i = 0; i < anArray.length; i++)
        System.out.print(anArray[i] + " ");

    SortArray.selectionSort(anArray, anArray.length);

    System.out.println("\nPrinting sorted array...");
}
/**
类,用于将可比较对象的数组从最小到最大排序
最大的。
*/
公营索塔雷酒店
{
/**将数组中的前n个对象按升序排序。
@param可比较对象的数组
@参数n一个大于0的整数*/

publicstatic您的方法需要的是对象数组而不是原语数组。因此int[]不起作用,而Integer[]起作用

public static void main(String[] args) {
  // int[] anArray = {15, 8, 10, 2, 5}; // given array
  Integer[] anArray = { 15, 8, 10, 2, 5 }; // given array

  System.out.println("Printing unsorted array...");
  for (int i = 0; i < anArray.length; i++)
     System.out.print(anArray[i] + " ");

  SortArray.selectionSort(anArray, anArray.length);

  System.out.println("\nPrinting sorted array...");
  for (int i = 0; i < anArray.length; i++)
     System.out.print(anArray[i] + " ");

}
publicstaticvoidmain(字符串[]args){
//int[]anArray={15,8,10,2,5};//给定数组
整数[]anArray={15,8,10,2,5};//给定数组
System.out.println(“打印未排序的数组…”);
for(int i=0;i

编辑:实际上,它需要的不仅仅是一个对象数组。它们还必须实现可比较的接口。

仔细查看您的声明:

public static <T extends Comparable<? super T>> void selectionSort(T[] a, int n)
第一个参数的类型为
int[]
,并且
int
不满足
T
的此要求

<> >而不是在代码> int []/CODE中传递,您可以考虑传递< <代码>整数[]/Cord>。<代码>整型< /COD>本身实现了<代码>可比较的,并且不需要改变很多代码以便使用它进行切换。只需将<代码> >数组> <代码>的声明从<代码> INT[] ]/COD> > <代码>整数[]。
可能已经起作用了。

当你说“我有麻烦了…”时,你的确切意思是什么?这并没有给我们提供多少我们可以帮助你的信息。
SortArray.selectionSort(anArray, anArray.length);