java - 通用数据类型参数

标签 java arrays generics parameters

我有这个类,它通过 selectionSort 处理对数组进行排序。 。 .我在主函数中对数组进行排序时遇到问题(附在底部)...调用 selctionSort() 的正确方法是什么?

我的问题:“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...");
}

最佳答案

您的方法需要一个对象数组而不是基元数组。所以 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] + " ");

}

编辑:实际上,它需要的不仅仅是一个对象数组。他们还必须实现 Comparable 接口(interface)。

关于java - 通用数据类型参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15539021/

相关文章:

c# - 通用类型 nullable 和 ToString

c# 泛型和非泛型方法之间的重载解析

java - java泛型中的边界扩展通配符

java - 缺少 Tomcat 8 的 Context.xml 文件

Liferay 的 Java 还是 Django?

java - Spring Boot : How to Disable JNDI lookup and use spring. 数据源代替测试?

c - 使用 C 将十六进制值存储到数组后打印

java - 由 : org. hibernate.QueryException 引起:无法解析属性:

Jquery 匹配值到数组

c - 将文件中的字符串存储到数组中