2013-03-21 3 views
0

selectionSort을 통해 정렬 정렬을 처리하는이 클래스가 있습니다. . . 내 주요 기능 (하단에 부착)에 배열을 정렬하는 데 문제가 있습니다 ... selctionSort()을 호출하는 올바른 방법은 무엇입니까?일반 데이터 형식 매개 변수

내 문제 : "형식 SortArrayselectionSort(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..."); 
} 
+3

"나는 문제가 있습니다 ..."라고 말하면 정확히 무엇을 의미합니까? 이것은 우리에게 당신을 도울 수있는 많은 정보를 제공하지 않습니다. –

답변

1

귀하의 방법은 객체의 배열이 아닌 기본 요소의 배열을 기대하고있다. 따라서 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] + " "); 

} 

편집 : 실제로는 Object의 배열 이상을 필요로합니다. 그들은 또한 Comparable 인터페이스를 구현해야합니다. 밀접하게 선언

+0

Ahhh ... 감사합니다. –

+0

@ user1527185 : 편집을 참조하십시오. –

0

봐 :

public static <T extends Comparable<? super T>> void selectionSort(T[] a, int n) 

T 필요 Comparable를 확장하는 유형으로.

SortArray.selectionSort(anArray, anArray.length); 

첫 번째 매개 변수 유형 int[]의, 그리고 intT이 요구 사항을 충족하지 않습니다 :

것은 당신이 그것을 사용하는 경우.

int[]을 전달하는 대신 Integer[]을 전달할 수 있습니다. Integer 자체는 Comparable을 구현하므로이를 사용하기 위해 많은 코드를 변경할 필요가 없습니다. 단순히 anArray의 선언을 int[]에서 Integer[]으로 변경하면 이미 작동 할 것입니다.