通过 String 对象在 ArrayList 中实现 binarySearch() Java

Implement binarySearch() in ArrayList by String object Java

如何对 ArrayList 使用二进制搜索?

ArrayList 的元素如下:

public class DictionaryElements implements 
Comparable<DictionaryElements>, Comparator<DictionaryElements>{

    private String word;
    private String translation;

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public String getTranslation() {
        return translation;
    }

    public void setTranslation(String translation) {
        this.translation = translation;
    }

    public DictionaryElements() {

    }

    public DictionaryElements(String word, String translation) {
        this.word = word;
        this.translation = translation;
    }

    @Override
    public String toString() {
        return word + " - " + translation;
    }

    @Override
    public int compareTo(DictionaryElements dictionary) {
        return this.word.compareTo(dictionary.word);
    }

    @Override
    public int compare(DictionaryElements wordOne, DictionaryElements wordTwo) {
        return wordOne.getWord().compareTo(wordTwo.getWord());
    }
}

比我在这里排序的列表:

public class DictionarySorter {

    ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();

    public DictionarySorter(ArrayList<DictionaryElements> dictionaryList) {
        this.dictionaryList = dictionaryList;
    }

    public ArrayList<DictionaryElements> getSortedByWord() {
        Collections.sort(dictionaryList);
        return dictionaryList;
    }
}

这里我试图暗示二进制搜索:

public class Main {
    public static void main(String[] args) {
        DictionaryElements dictionaryElements = new DictionaryElements();
        ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
        DictionarySorter dictionarySorter = new 
        DictionarySorter(dictionaryList);
        boolean found = true;
        dictionaryList();

        Scanner scanner = new Scanner(System.in);
        System.out.println("Write one word in English:");
        String wordInEnglish = scanner.nextLine();

        int index = Collections.binarySearch(dictionaryList, wordInEnglish);
    
    if (found) {
     //code here.
    }
    else {
        System.out.println("Sorry, i didn't find " + wordInEnglish + " ;(");
    }
    scanner.close();
}

public static void dictionaryList() {
    ArrayList<DictionaryElements> dictionaryList = new ArrayList<>();
    dictionaryList.add(new DictionaryElements("Apple", "Apfel"));
    dictionaryList.add(new DictionaryElements("Pear", "Birne"));
    dictionaryList.add(new DictionaryElements("Orange", "Orange"));

    DictionarySorter dictionarySorter = new DictionarySorter(dictionaryList);
    ArrayList<DictionaryElements> sortedDictionaryList = dictionarySorter.getSortedByWord();
    for (DictionaryElements dictionary : sortedDictionaryList) {
        System.out.println(dictionary);
        }
     }
}

错误说: 类型 Collections 中的方法 binarySearch(List>, T) 不适用于参数 (ArrayList, String)

我错过了什么,我该如何解决?

您有一个包含 DictionaryElements 个对象的列表,并且正在寻找一个字符串。

这就像我递给你一袋苹果然后问:嘿,给我找这个梨。

编译器正在帮助您并阻止您编写此代码,因为它没有任何意义。

如果您查看 Collections.binarySearch 的方法声明,则实施的比较器必须与传入的键的类型相同。

您的 DictionaryElements 扩展了 DictionaryElements 类型的 Comparable,但您传入的键是 String 类型。

您需要传递一个 DictionaryElement 作为键:

DictionaryElements key = new DictionaryElements(wordInEnglish, translation);
int index = Collections.binarySearch(dictionaryList, key);

https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#binarySearch(java.util.List,%20T)