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
,但是当我输入 _
时,然后用 o
code> 或 s
它向我显示了其他类似 dce_offset
的内容,其中偏移量来自列表中的 words.add("fragoffset");
某处。
我可以做什么来解决这个问题?先感谢您。
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 works 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能是因为代码中的这些行:
_
不是字母字符,因此它将其视为空格。您可以尝试将条件更改为:It's probably because of these lines in the code:
_
is not a letter character, so it treats it the same way as a space. You can try changing the condition to:我想你必须在这里添加下划线作为“字母”
I guess you have to add the underscore as "letter" here
它与
insertUpdate()
中的这一部分有关:具体来说,
Character.isLetter()
对于下划线字符返回 false。这意味着单词在下划线位置之后开始。要解决这个问题,您需要修改
if
语句以允许在单词中使用任何非字母字符。您可以显式检查“_”或使用Chapter.isWhiteSpace()
来包含所有非空格、制表符或换行符的字符。It has to do with this section in
insertUpdate()
: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 useChacter.isWhiteSpace()
to include all characters that aren't a space, tab or newline.