java - Collections.binarySearch (Java) 中下划线(_) 的问题

标签 java list collections binary-search

问题:

我正在使用Java Tutorials™这个的源代码。这是source code.

我试过这个:

--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_iface当我输入dce时,但是当我输入_时然后跟随 os它向我显示了其他类似 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 == '_')) {

关于java - Collections.binarySearch (Java) 中下划线(_) 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714353/

相关文章:

java - 在迭代 ConcurrentHashMap 时添加和删除值

java - 在 JPA 查询中包含枚举

java - 其他具有编译时/运行时 "mixins"功能的框架,例如 Spring Roo for Java

java - 在 Eclipse 中运行程序时播放音乐,但在运行 Jar 可执行文件时不播放

python - 如何向列表的每个值添加一个字符串,然后将该字符串的值加起来?

Python错误: Int object not subscriptable

python - 比较python中的2个字典并创建一个包含字典值的列表

java - 如何根据列表中的两个不同值获取 2 个计数并检查两个计数是否相等?

java - 为什么我不能在这段代码中使用 Collections.max() 函数? - java

java - 值必须存储在 EMF 模型中,但不应传播到 xml 文件