Let's assume, you enter the text, Hello
.
(假设您输入文本Hello
。)
Length of the text, Hello
is 5
(文字长度, Hello
为5
)
int high = s.length() - 1
means you are storing 4
(ie 5 - 1) to the variable, high
.
(int high = s.length() - 1
表示您要将4
(即int high = s.length() - 1
存储到变量high
。)
The index of the first character in a String
is 0
. (String
第一个字符的索引为0
。)
Therefore, the index of H
in Hello
is 0
, the index of e
is 1
and so on. (因此,索引H
在Hello
是0
,的指数e
是1
等。)
Thus the index of the last character, o
is 4
which is equal to "Hello".length() - 1
. (因此,最后一个字符的索引o
为4
,等于"Hello".length() - 1
。)
In the first iteration of the while
loop, the condition, if (s.charAt(low) != s.charAt(high))
, compares the first character (ie H
) with the last character (ie o
) of the text, Hello
;
(在while
循环的第一次迭代中,条件if (s.charAt(low) != s.charAt(high))
将文本的第一个字符(即H
)与最后一个字符(即o
)进行比较, Hello
;)
because the value of low
is 0
and that of high
is 4
. (因为low
值为0
, high
值为4
。)
I recommend you understand the basics of Java from a good tutorial eg https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html
(我建议您从一个好的教程中了解Java的基础知识,例如https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…