2014-02-25 2 views
2

전 교수진이 있으며, 삽입 분류를 사용하고 싶습니다. 난에서 오류가 발생합니다 :오브젝트 배열을 사용한 삽입 정렬?

InsertionSort.insertionSortA(faculty, faculty.length); 

오류은 말한다 : "방법 insertionSortA 클래스 삽입 정렬에 주어진 타입에 적용 할 수 없습니다
필요 : 대등 [], int로
발견 학부 [], INT

InsertionSort.insertionSortA((Comparable[]) faculty, faculty.length); 

나는 내가 가진 경우 정수 [] 배열이 작동 것이라고 알고 있지만, 나는 왜 내 학부 [] 료를 혼란 스러워요 :
나는이 일을하는 것은 작동하지 않을 것이라는 점을 알고있다 일하지 않아?

public class Tester { 

public static void main(String[] args) { 

    InsertionSort insertionSort = new InsertionSort(); 
    Education edu = new Education ("BA", "Business", 1); 
    Education edu2 = new Education ("BA", "Health Science", 1); 
    Education edu3 = new Education ("BA", "Computer Science", 1); 

    Faculty[] faculty = new Faculty[] { 

    new Faculty ("538", "Doe", "Jane", 'M', 1994,1,10, 
    "Assistant", edu), 
    new Faculty ("238", "Do", "John", 'F', 1994,6,1, 
    "Assistant", edu2), 
    new Faculty ("080", "White", "Snow", 'F', 1994,4,22, 
    "Full", edu3) 
    }; 

    InsertionSort.insertionSortA(faculty, faculty.length); 
} 

} 요소 수, 비교 논리는 2 교수진을 비교하는 방법의 기초이다 (비교해야 배열/컬렉션 삽입 정렬을 적용하기 위해

public class InsertionSort { 

public static void insertionSortA(Comparable[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Comparable nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) > 0)) { 
      theArray[loc] = theArray[loc-1]; 
      loc--; 
     } // end while 
     theArray[loc] = nextItem; 
    } // end for 
} // end insertionSort 
public static void insertionSortB(Comparable[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Comparable nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].compareTo(nextItem) < 0)) { 
      theArray[loc] = theArray[loc-1]; 
      loc--; 
     } // end while 
     theArray[loc] = nextItem; 
    } // end for 
} 
} 

답변

3

이름, 나이, 급여 등을 기준으로 함).

zero    : If object is same as the specified object (f) 
positive integer : If object is greater than the specified object (f) 
negative integer : If object is less than the specified object (f) 

이의 문서를 참조하십시오 :

public class Faculty implements Comparable<Faculty> 
{ 
    public int compareTo(Faculty f) 
    { 

    // comparison logic is on the basis of how you want to compare 2 faculty members 
    // you might want to compare name, salaries etc 

    } 
} 

은 compareTo() 함수는 다음을 반환해야합니다 :이 다음과 같이) (학부 클래스에서의 Comparable 인터페이스를 구현하고 기능은 compareTo를 정의하여 수행 할 수 있습니다 다음 link

2

Faculty 클래스를 보지 않고서는 문제를 추측 할 수 있습니다.

FacultyComparable을 구현하지 않은 것처럼 보입니다. Integer 인 경우 이고, Comparable 인 경우 Integer[]으로 함수를 호출 할 수 있습니다.

는 당신이 반환해야하는지 마찬가지로 지금까지 compareTo(Faculty)

public int compareTo(Faculty faculty) { 
    //... 
} 

를 재정 의하여, 당신의 Faculty 클래스에서 Comparable<Faculty>를 구현해야합니다

, 당신은 API을 검토해야합니다. 그러나 일반적으로 정확히 같으면 0을 반환하고 이 faculty (무엇이든 의 경우 "큰"을 의미 함)을 정의하면 0보다 큰 값을 반환해야합니다. 이보다 엄격한 규칙은 없습니다 그 외에도 항상 일관된 가치를 반환합니다.

0

여전히이 페이지에서 문제가있는 경우, 대체 솔루션은 Comparable 인터페이스를 무시하는 것입니다. 객체가 ID, 이름, 성 또는 시작 연도와 같은 둘 이상의 속성에 의해 정렬 될 수있는 많은 상황이 있기 때문에. 이 경우 InsertionSort 클래스의 각 정렬 특성에 대해 별도의 메서드를 만듭니다. 또한 정렬 메소드 입력 매개 변수 유형을 Comparable []에서 Faculty []로 변경해야합니다.

public class InsertionSort { 

    public static void byLastName(Faculty[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Faculty nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].getLastName().compareTo(nextItem.getLastName()) > 0)) { 
     theArray[loc] = theArray[loc-1]; 
     loc--; 
     } 
     theArray[loc] = nextItem; 
    } 
    } 

    public static void byFirstName(Faculty[] theArray, int n) { 

    for (int unsorted = 1; unsorted < n; ++unsorted) { 
     Faculty nextItem = theArray[unsorted]; 
     int loc = unsorted; 
     while ((loc > 0) &&(theArray[loc-1].getFirstName().compareTo(nextItem.getFirstName()) > 0)) { 
     theArray[loc] = theArray[loc-1]; 
     loc--; 
     } 
     theArray[loc] = nextItem; 
    } 
    } 
} 

당신은 다음과 같이 당신의 학부 배열을 정렬 할 수 있습니다 : 정렬 blogpost 내 체크 아웃 삽입하여 객체를 정렬에 대한 자세한 설명은

InsertionSort.byLastName(faculty, faculty.length); 

또는

InsertionSort.byFirstName(faculty, faculty.length); 

. Java, C++, Python 및 Javascript로 구현됩니다.