为什么使用\0将highEndPoint包含在子列表中 [英] why use \0 to include highEndPoint as part of the sublist

查看:106
本文介绍了为什么使用\0将highEndPoint包含在子列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

编辑:

此外,会发生什么如果变量 dictionary 是对SortedSet的引用?如果我想包含highEndPoint,我现在该怎么办?

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

推荐答案

subSet(a,b)包含 a 独家 b 。因此,如果要查找包含上限 pickle 的子集,则必须在 pickle 作为(独占)上限。

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.

你可以通过添加空字符来获得这个 \0 到字符串的末尾。

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

在Java中, char 真的只是 0 65535 ,以及<$ c的 compareTo 方法$ c> String 按这些值对字符进行排序。要查看 \0 是可能的最小 char 值,您可以像这样打印其值:

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

这篇关于为什么使用\0将highEndPoint包含在子列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆