簡體   English   中英

如何使用Collections.binary搜索ArrayList <list<string> > 到達 java </list<string>

[英]How to use Collections.binarySearch for ArrayList<List<String>> arr in java

我有一個ArrayList<List<String>>並且我正在嘗試在集合中使用內置的二進制搜索,但我總是會遇到錯誤

int index = Collections.binarySearch(arraylistdata, id);

其中arraylistdata是我的List<String>ArrayList

public class CollectionsTest {
    
    @Test
    void test() {
        List<String> list = List.of("a", "b", "c");
        int index = Collections.binarySearch(list, "b");
        Assertions.assertEquals(1, index);
    }
}

這工作正常,但你需要知道Collections#binarySearch接受List<? extends Comparable<? super T>> List<? extends Comparable<? super T>> List<? extends Comparable<? super T>>作為它的第一個參數。

在您的情況下, ArrayList<List<String>>不符合要求,因為List<String>List<?>本身不是Comparable object。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM