簡體   English   中英

Collections.binarySearch(Java)中的下划線(_)問題

[英]Problem with underscore(_) in Collections.binarySearch (Java)

問題:

我正在為此使用Java Tutorials™源代碼。 這是源代碼。

我嘗試了這個:

--following with another section of sorted words--
words.add("count");
words.add("cvs");
words.add("dce"); 
words.add("depth");
--following with another section of sorted words--

而且效果很好。 但是,當我使用此:

--just a section of sorted words--
words.add("count");
words.add("cvs");
words.add("dce_iface"); 
words.add("dce_opnum");
words.add("dce_stub_data");
words.add("depth");
--following with another section of sorted words--

當我鍵入dce ,它的確顯示了dce_iface ,但是當我鍵入_然后跟os它向我顯示了類似dce_offset其他dce_offset ,其中偏移量來自words.add("fragoffset"); 在列表中的某處。

我該怎么做才能解決這個問題? 先感謝您。

可能是因為代碼中的這些行:

for (w = pos; w >= 0; w--) {
    if (! Character.isLetter(content.charAt(w))) {
        break;
    }
}

_不是字母字符,因此將其與空格相同。 您可以嘗試將條件更改為:

char c = content.charAt(w);
if (! (Character.isLetter(c) || c == '_')) {

我猜你必須在這里添加下划線作為“字母”

        // Find where the word starts
        int w;
        for (w = pos; w >= 0; w--) {
            if (!Character.isLetter(content.charAt(w))) {
                break;
            }
        }

它與insertUpdate()此部分insertUpdate()

// Find where the word starts
int w;
for (w = pos; w >= 0; w--) {
    if (! Character.isLetter(content.charAt(w))) {
        break;
    }
}

具體來說, Character.isLetter()對於下划線字符返回false。 這意味着單詞在下划線位置之后開始。

要解決此問題,您需要修改if語句以允許您要在單詞中使用任何非字母字符。 您可以顯式檢查'_'或使用Chacter.isWhiteSpace()包括所有非空格,制表符或換行符的字符。

暫無
暫無

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

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