Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java “;-97”;在下面的代码中是什么意思?_Java_String_Search_Linked List_Contains - Fatal编程技术网

Java “;-97”;在下面的代码中是什么意思?

Java “;-97”;在下面的代码中是什么意思?,java,string,search,linked-list,contains,Java,String,Search,Linked List,Contains,在下面的代码中“-97”是什么意思 if (dictionary[(int) word.charAt(0) -97 ].contains(word)) 我们创建了一个由26个LinkedList组成的数组来模拟字典。 每个列表包含以“a”、“b”、“c”开头的所有单词…。“z”。 代码由讲师给出 以下是随附的说明: 在特定MyLinkedList中搜索单词的步骤 假设要搜索的单词位于名为wordstr的字符串类型变量中 dictionary [(int)wordstr.charAt(0) -

在下面的代码中“-97”是什么意思

if (dictionary[(int) word.charAt(0) -97 ].contains(word))
我们创建了一个由26个LinkedList组成的数组来模拟字典。 每个列表包含以“a”、“b”、“c”开头的所有单词…。“z”。 代码由讲师给出

以下是随附的说明:

在特定MyLinkedList中搜索单词的步骤

假设要搜索的单词位于名为wordstr的字符串类型变量中

dictionary [(int)wordstr.charAt(0) - 97].contains(wordstr) ; 
将允许您跳转到正确的链接列表,并且根据单词是否在列表中,contains将返回true/false


我只是不明白为什么“-97”

97是字符“a”的数值,所以如果你从“a”和“z”之间的字符中减去97,你就是在将该字符映射到数组的0到25之间的索引。

谢谢你。。。那么表达式[(int)wordstr.charAt(0)–97]的计算结果是字典数组的索引?如果wordstr是“apple”,那么[(int)wordstr.charAt(0)–97]表示(wordstr“apple”的索引0处的字符]?即,97-97=0,这是索引。那么“boy”是index=(98-97)=1吗?@greg因为你刚才的困惑,我们通常称这些数字为“magic”。与
(int)word.charAt(0)-97不同,部分可以写成
(int)wordstr.charAt(0)-'a'
,这可能更可读(如果您知道可以在Unicode表中减去字符以获得表示其距离的整数)。非常有见地。谢谢你们两位。是的,(int)wordstr.charAt(0)-“a”更有意义。