为什么使用 \0 将 highEndPoint 包含在子列表中

why use \0 to include highEndPoint as part of the sublist

我从 java 教程 oracle 中看到了下面的代码。为了统计doorbell(含)到pickle(含)之间的字数,作者在pickle字后加了[=13=]。我知道在 pickle 之后添加 [=13=] 的效果是 pickle 这个词现在作为子集的一部分包含在内。但我的问题是,为什么要使用 [=13=]?有人可以帮帮我吗?在此先感谢您的帮助!

SortedSet<String> dictionary = new TreeSet<>(entire collection of words from a dictionary);
int count = dictionary.subSet("doorbell", "pickle[=10=]").size();
System.out.println(count);

编辑:

此外,如果变量 dictionary 是对 SortedSet 的引用会怎样?如果我想包含highEndPoint,我现在应该怎么做?

subSet(a, b) 包含 a 不包含 b。因此,如果你想找到一个包含上限 pickle 的子集,你必须使用 pickle 之后的下一个可能的字符串作为(不包括)上限。

您可以通过在字符串的末尾添加空字符 [=16=] 来获得它。

在Java中,一个char是"really"只是一个介于065535之间的整数,而compareTo方法String 按这些值对字符进行排序。要查看 [=16=] 是可能的最小 char 值,您可以像这样打印它的值:

 System.out.println((int) '[=10=]');    // Prints 0

我相信 [=12=] 是空字符。在 ASCII 词典中,从单词 pickle 之后的词典点开始的下一个单词是 pickle[=14=]SortedSet.subset()JavaDoc 有以下说法:

Returns a view of the portion of this set whose elements range from fromElement, inclusive, to toElement, exclusive.

换句话说,如果您的代码片段如下:

int count = dictionary.subSet("doorbell", "pickle").size();

那么pickle这个词就不会出现在子集中。

一个更好的例子可能是如果你想得到所有 pickles 形式的词,但没有 picklet 形式的词(以 t 结尾),那么你将使用以下代码:

int count = dictionary.subSet("doorbell", "picklet").size();

Here is a link转为ASCII字符table.