简体   繁体   English

Java“比较无法解析为类型”错误

[英]Java “compare cannot be resolved to a type” error

I'm getting a strange error when attempting to use a comparator with a binary search on an array. 尝试在数组上使用比较器进行二进制搜索时遇到一个奇怪的错误。 The error states that "compareArtist cannot be resolved to a type" and is thrown by Eclipse on this code: 错误指出“ compareArtist无法解析为类型”,并且由Eclipse在此代码上抛出:

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

I've done some searching and found references to a possible bug with Eclipse, although I have tried the code on a different computer and the error persists. 尽管已经在另一台计算机上尝试了该代码,但错误仍然存​​在,但我已经进行了一些搜索并找到了有关Eclipse可能存在的错误的参考。

I've also found similar issues regarding the capitalization of the compare method, in this case compareArtist. 我还发现了关于compare方法大写的类似问题,在本例中是compareArtist。 I've seen examples where the first word in the method name is capitalized, although it was my understanding that method names are traditionally started with a lower case letter. 我看过一些示例,其中方法名称中的第一个单词大写,尽管据我了解,方法名称传统上以小写字母开头。 I have experimented with changing the capitalization but nothing has changed. 我已经尝试过更改大小写,但是没有任何变化。

I have also found references to this error if the class doesn't import the correct package. 如果该类未导入正确的包,我也找到了对此错误的引用。 I have imported java.util in both classes in question, which to my knowledge allows the use of the Comparator. 我已经在两个相关的类中都导入了java.util,据我所知,它允许使用Comparator。

I've experimented with writing the compareArtist method within the class that has the binary search call as well as in the "Song" class, which according to my homework assignment is where it should be. 我已经尝试过在具有二进制搜索调用的类以及“ Song”类中编写compareArtist方法,根据我的作业分配,该类应该在该位置。 I've changed the constructor accordingly and the issue persists. 我已经相应地更改了构造函数,问题仍然存在。

Lastly, I've attempted to override the Comparator compare method by implementing Comparator in the Song class and creating my own method called "compare". 最后,我试图通过在Song类中实现Comparator并创建自己的方法“ compare”来覆盖Comparator比较方法。 This returns the same error. 这将返回相同的错误。 I've only moved to calling the comparator method something different than "compare" after finding several examples that do the same. 在找到几个相同的示例之后,我只想调用比较器方法而不是“比较”。

Here is the relevant code for the class that calls the binary search that uses the comparator. 这是调用使用比较器的二进制搜索的类的相关代码。 This code also has a local version of the compareArtist method. 此代码还具有compareArtist方法的本地版本。 While it is not being called currently, the code for this method is the same as the in the class Song, where I am trying to call it from. 尽管当前未调用此方法,但是此方法的代码与Song类中的相同,我试图从中调用它。

Thanks for any advice and insight. 感谢您的任何建议和见解。

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);

Code for Song class: 歌曲课程代码:

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());
   }

There is also a main() method at the end of the class, but it only provides internal testing code and isn't relevant to the problem at hand. 类的最后还有一个main()方法,但是它仅提供内部测试代码,与当前问题无关。

In this code: 在此代码中:

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

you are instantiating from Song.compareArtist static inner class. 您是从Song.compareArtist静态内部类实例化的。 This error message says it cannot find this class. 此错误消息说它找不到此类。 For this to work, you must change your Song class in this way: 为此,您必须通过以下方式更改Song类:

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;
    }

  }

}

Your statement: 您的声明:

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

Means that there is inner class named compareArtist in class Song . 意味着在Song类中有一个名为compareArtist内部类。 But I think you only want to use the method named compareArtist in class SearchByArtistPrefix . 但我认为你只需要使用命名方法compareArtistSearchByArtistPrefix If it is so, You should do it as follow , and use anonymous inner class. 如果是这样,则应按照以下步骤进行操作,并使用匿名内部类。

First, Remove the compareArtist method 首先,删除compareArtist方法

Second, Implement the Comparator interface 二,实现Comparator接口

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

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

};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM