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

眉间皱痕 提交于 2019-12-02 01:19:56

问题


I saw the code below from java tutorial oracle. In order to count the number of words between doorbell (inclusive) and pickle(inclusive), the author added \0 after the word pickle. I understand that the effect of adding \0 after pickle, is that the word pickle is now included as part of the subset. But my question is, why use \0? Could someone please help me out? Thanks in advance for any help!

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

Edit:

Also, what happen if the variable dictionary is a reference to SortedSet? what should I do now if I want to include the highEndPoint?


回答1:


subSet(a, b) is inclusive of a but exclusive of b. Therefore if you want to find a subset that is inclusive of the upper bound pickle, you have to use the next possible string after pickle as the (exclusive) upper bound.

You can get this by adding the null character \0 to the end of the string.

In Java, a char is "really" just an integer between 0 and 65535, and the compareTo method of String sorts characters by these values. To see that \0 is the smallest possible char value you can print its value like this:

 System.out.println((int) '\0');    // Prints 0



回答2:


I believe \0 is the null character. The next word in the ASCII dictionary from a lexigraphic point after the word pickle is pickle\0. The JavaDoc for SortedSet.subset() has the following to say:

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

In other words, if your code snippet were the following:

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

then the word pickle would not appear in the subset.

A better example might be if you wanted to get all words of the form pickles, but nothing of the form picklet (ending in t), then you would use the following code:

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

Here is a link to the ASCII character table.



来源:https://stackoverflow.com/questions/36121685/why-use-0-to-include-highendpoint-as-part-of-the-sublist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!