开发者

Problem with underscore(_) in Collections.binarySearch (Java)

开发者 https://www.devze.com 2022-12-28 09:48 出处:网络
Problem: I am using Java Tutorials™ sourcecode for this. This is the source code. I tried this: --following with another section of sorted words--

Problem:

I am using Java Tutorials™ sourcecode for this. This is the source code.

I tried this:

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

and it wor开发者_高级运维ks perfectly. However when I use this:

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

It does show dce_iface when I type dce, but when I type _ then following with o or s it shows me something else like dce_offset where the offset comes from words.add("fragoffset"); somewhere in the list.

What can I do to solve this problem? Thank you in advance.


It's probably because of these lines in the code:

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

_ is not a letter character, so it treats it the same way as a space. You can try changing the condition to:

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


I guess you have to add the underscore as "letter" here

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


It has to do with this section in insertUpdate():

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

Specifically, Character.isLetter() returns false for the underscore character. That means that the word starts after the underscore position.

To solve it, you need to modify the if statement to allow any non letter characters you want to use in the words. You could explicitly check for '_' or use Chacter.isWhiteSpace() to include all characters that aren't a space, tab or newline.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号