2012-09-25 3 views
1

배열에 대해 이진 검색을 사용하여 비교자를 사용하려고하면 이상한 오류가 발생합니다. 나는 시도하고 있지만, 나는 약간의 검색을 수행하고 이클립스 가능한 버그에 대한 참조를 발견했습니다Java "형식을 확인할 수없는 경우"오류

Comparator<Song> compare = new Song.compareArtist(); 

:이 오류는 "compareArtist이 유형에 해결 될 수없는"이 코드를 이클립스에 의해 발생되는 상태 코드를 다른 컴퓨터에 저장하면 오류가 계속 발생합니다.

나는이 경우에도 compareArtist와 같은 compare 메소드의 대문자 사용과 관련하여 비슷한 문제를 발견했습니다. 메쏘드 이름이 전통적으로 소문자로 시작되었다는 것을 이해하고 있었지만 메쏘드 이름의 첫번째 단어가 대문자로 쓰인 예제를 보았습니다. 나는 대문자의 변경을 실험했지만 아무 것도 변하지 않았다.

클래스가 올바른 패키지를 가져 오지 않으면이 오류에 대한 참조를 발견했습니다. 두 클래스 모두에서 java.util을 가져 왔으며, 필자의 지식에 따르면 Comparator를 사용할 수 있습니다.

필자는 숙제에 따라 이진 검색 호출과 "노래"클래스가있는 클래스 내에 compareArtist 메서드를 작성하는 실험을했습니다. 그에 따라 생성자를 변경 했으므로 문제가 지속됩니다.

마지막으로, 저는 Comparator를 Song 클래스에 구현하고 "compare"라는 자체 메서드를 작성하여 Comparator 비교 메서드를 재정의하려고했습니다. 동일한 오류가 반환됩니다. 필자는 같은 것을하는 몇 가지 예제를 찾은 후에 "비교"와 다른 뭔가를 비교 메서드를 호출하는 것으로 옮겼습니다.

다음은 비교자를 사용하는 이진 검색을 호출하는 클래스의 관련 코드입니다. 이 코드에는 compareArtist 메소드의 로컬 버전도 있습니다. 현재 호출되지는 않지만이 메서드의 코드는 Song 클래스에서와 동일합니다. 여기서이 코드는 호출하려고합니다.

조언과 통찰력을 주셔서 감사합니다. 송 클래스에 대한

import java.io.*; 
import java.util.*; 

public class SearchByArtistPrefix { 

    private Song[] songs; // keep a direct reference to the song array 
    private Song[] searchResults; // holds the results of the search 
    private ArrayList<Song> searchList = new ArrayList<Song>(); // hold results of  search while being populated. Converted to searchResults array. 

    public SearchByArtistPrefix(SongCollection sc) { 
     songs = sc.getAllSongs(); 
    } 

    public int compareArtist (Song firstSong, Song secondSong) { 
     return firstSong.getArtist().compareTo(secondSong.getArtist()); 
    } 

    public Song[] search(String artistPrefix) { 

     String artistInput = artistPrefix; 
     int searchLength = artistInput.length(); 

     Song searchSong = new Song(artistInput, "", ""); 
     Comparator<Song> compare = new Song.compareArtist(); 
     int search = Arrays.binarySearch(songs, searchSong, compare); 

코드 :

import java.io.FileNotFoundException; 
import java.util.*; 

public class Song implements Comparable<Song> { 

    private String artist, title, lyrics; 
    private static int compareCounter = 0; 

    public Song(String artistName, String songTitle, String songLyrics) { 
     this.artist = artistName; 
     this.title = songTitle; 
     this.lyrics = songLyrics; 
    } 
    public String getArtist() { 
     return artist; 
    } 

    public void setArtist(String artist) { 
     this.artist = artist; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getLyrics() { 
     return lyrics; 
    } 

    public void setLyrics(String lyrics) { 
     this.lyrics = lyrics; 
    } 

    public static int getCounter() { 
     return compareCounter; 
    } 

    public static void setCounter(int compareCounter) { 
     Song.compareCounter = compareCounter; 
    } 

    public int compareTo(Song secondSong) { 

     String secondArtist = secondSong.getArtist(); 
     String secondTitle = secondSong.getTitle(); 

     compareCounter++; 

     if (!this.artist.equals(secondArtist)) { 
     return this.artist.compareToIgnoreCase(secondArtist); 
     } else { 
     return this.title.compareToIgnoreCase(secondTitle); 
     } 
    } 

    public int compareArtist (Song firstSong, Song secondSong) { 
     return firstSong.getArtist().compareTo(secondSong.getArtist()); 
    } 

또한 클래스의 끝에서 main() 메소드가 있지만, 그것은 단지 내부 테스트 코드를 제공하고 문제에 관련이 없습니다 손. 이 코드에서

+0

PLZ 여기에 노래 코드를 넣어 Comparator 인터페이스를 구현의 compareArtist 방법을

두 번째를 제거합니다. – RGO

답변

1

:

Comparator<Song> compare = new Song.compareArtist(); 

당신이 Song.compareArtist 정적 내부 클래스에서 인스턴스화된다. 이 오류 메시지는이 클래스를 찾을 수 없다고 말합니다. 이 작업을 위해, 당신은이 방법으로 노래 클래스를 변경해야합니다

class Song{ 

    public static class compareArtist<T> implements Comparator<T> { 

    @Override 
    public int compare(Object o1, Object o2) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    } 

} 
+1

감사합니다. 비교기가 작동하도록 Song 클래스에 하위 클래스를 만들어야한다는 것을 알지 못했습니다. 이제 메인 클래스에서 getter를 전달하는 방법을 알아 내야 만합니다 ... –

+0

서브 클래스 내에서 getter를 다시 만들었습니다 ... –

+0

그러면 upvote를 누르십시오! :-) – RGO

0

귀하의 문을 :

Comparator<Song> compare = new Song.compareArtist(); 

클래스 SongcompareArtist라는 이름의 내부 클래스가 있다는 것을 의미합니다. 하지만 SearchByArtistPrefix 클래스에 compareArtist이라는 메서드 만 사용하려고한다고 생각합니다. 그렇다면 익명의 내부 클래스를 사용하여 다음과 같이 수행해야합니다.

첫째, 너무,

Comparator<Song> compare = new Comparator<Song>(){ 

    public int compareTo(Song anotherSong){ 
     //Implement your compare logic here 
    } 

}; 
관련 문제