2012-11-30 24 views
0

我無法弄清楚如何更正程序。我已經嘗試將Sorting.java程序更改爲public static void selectionSort(int [] intList),但似乎沒有解決它。有人可以幫忙嗎?多態分類轉換

文件Sorting.java包含Sorting類。該類實現了選擇排序和插入排序算法,以按升序對任何Comparable對象數組進行排序。在本練習中,您將使用Sorting類對幾種不同類型的對象進行排序。

  1. 文件Numbers.java在整數數組讀取,調用選擇排序算法對它們進行排序,然後打印排序後的數組。將Sorting.java和Numbers.java保存到您的目錄中。 Numbers.java不會以其當前形式進行編譯。研究它,看看你是否可以找出原因。

  2. 嘗試編譯Numbers.java並查看錯誤消息是什麼。 **問題涉及基本數據和對象之間的差異。改變程序,使其能正常工作(注意:你不需要做很多改變 - Java 1.5的自動裝箱功能將關注從int到Integer的大部分轉換)。

下面的代碼: -

// Demonstrates the selection sort and insertion sort algorithms. 
public class Sorting { 
    // Sorts the specified array of objects using the selection 
    // sort algorithm. 

    public static void selectionSort (Comparable[] list) { 
     int min; 
     Comparable temp; 
     for (int index = 0; index < list.length-1; index++) { 
      min = index; 
      for (int scan = index+1; scan < list.length; scan++) 
       if (list[scan].compareTo(list[min]) < 0) 
        min = scan; 

     // Swap the values 
      temp = list[min]; 

      list[min] = list[index]; 
      list[index] = temp; 
     } 
    } 

    // Sorts the specified array of objects using the insertion 
    // sort algorithm. 
    public static void insertionSort (Comparable[] list) { 
     for (int index = 1; index < list.length; index++) { 
      Comparable key = list[index]; 
      int position = index; 

      // Shift larger values to the right 
      while (position > 0 && key.compareTo(list[position-1]) < 0) { 
       list[position] = list[position-1]; 
       position--; 
      } 

      list[position] = key; 
     } 
    } 
} 

// Numbers.java  
// Demonstrates selectionSort on an array of integers.  
import java.util.Scanner; 

public class Numbers { 

    // Reads in an array of integers, sorts them, 
    // then prints them in sorted order. 
    public static void main (String[] args) { 
     int[] intList; 
     int size; 
     Scanner scan = new Scanner(System.in); 
     System.out.print ("\nHow many integers do you want to sort? "); 

     size = scan.nextInt(); 
     intList = new int[size]; 

     System.out.println ("\nEnter the numbers..."); 
     for (int i = 0; i < size; i++) 
      intList[i] = scan.nextInt(); 

     Sorting.selectionSort(intList); 

     System.out.println ("\nYour numbers in sorted order..."); 
     for (int i = 0; i < size; i++) 
      System.out.print(intList[i] + " "); 

     System.out.println(); 
    } 
} 
+1

爲什麼這麼多的每行之間的空間?閱讀這樣的代碼是非常困難的。請格式化您的代碼以刪除多餘的空格。 –

回答

1
在排序類 selectionSort(Comparable[] list)方法期待可比陣列

但是你正在發送int [],而不是int[]你可以發送Integer[]

main方法聲明數組如下,它將正常工作。

Integer [] intList;

用下面的代碼替換你的主要方法。

public static void main(String[] args) 

    { 

     Integer[] intList; 

     int size; 

     Scanner scan = new Scanner(System.in); 

     System.out.print("\nHow many integers do you want to sort? "); 

     size = scan.nextInt(); 

     intList = new Integer[size]; 

     System.out.println("\nEnter the numbers..."); 

     for (int i = 0; i < size; i++) 

      intList[i] = scan.nextInt(); 

     Sorting.selectionSort(intList); 

     System.out.println("\nYour numbers in sorted order..."); 

     for (int i = 0; i < size; i++) 

      System.out.print(intList[i] + " "); 

     System.out.println(); 

    } 

FYI

雖然int被autoboxed到Integer,但int[]不Autoboxed到Integer[]

數組不是裝箱的,只是類型本身。

看到這個:How to convert int[] into List<Integer> in Java?

中的Java

+0

非常感謝! –