我无法理解Java 11中的lastIndexOf方法。

huangapple go评论59阅读模式
英文:

I cant understand this lastindexof method in Java 11

问题

正索引和负索引在Java的indexOf方法中用于搜索字符串中特定字符的位置。

正索引表示从字符串的开头开始计算索引位置。例如,当你使用indexOf("d", 0)时,它会从字符串的第一个字符开始查找字符"d",然后返回找到的第一个匹配的位置。在你的示例中,无论从哪个正索引位置开始搜索,结果都是3,因为在字符串"abcd"中字符"d"的索引位置是3。

负索引表示从字符串的末尾开始计算索引位置。例如,当你使用indexOf("d", -1)时,它会从字符串的末尾开始向前查找字符"d",然后返回找到的第一个匹配的位置。在你的示例中,无论从哪个负索引位置开始搜索,结果都是3,因为从字符串末尾向前查找字符"d"时,也会找到索引位置3的字符"d"。

总结一下,正索引从字符串开头开始计算索引位置,而负索引从字符串末尾开始计算索引位置,但它们都用于在字符串中查找特定字符的位置。在你的示例中,负索引的搜索结果与正索引的搜索结果相同,因为字符串"abcd"中只有一个字符"d",它的位置是3。

英文:
    int a="abcd".indexOf("d",0);
    int b="abcd".indexOf("d",1);
    int c="abcd".indexOf("d",2);
    int d="abcd".indexOf("d",3);
    int e="abcd".indexOf("d",4);
    int f="abcd".indexOf("d",5);




    int b1="abcd".indexOf("d",-1);
    int c1="abcd".indexOf("d",-2);
    int d1="abcd".indexOf("d",-3);
    int e1="abcd".indexOf("d",-4);
    int f1="abcd".indexOf("d",-5);


    System.out.println("Last index of from index 0 "+a);
    System.out.println("Last index of from index 1 "+b);
    System.out.println("Last index of from index 2 "+c);
    System.out.println("Last index of from index 3 "+d);
    System.out.println("Last index of from index 4 "+a);
    System.out.println("Last index of from index 5 "+f);


    System.out.println("Last index of from index -1 "+b1);
    System.out.println("Last index of from index -2 "+c1);
    System.out.println("Last index of from index -3 "+d1);
    System.out.println("Last index of from index -4 "+e1);
    System.out.println("Last index of from index -5 "+f1);

Results

Last index of from index 0 3
Last index of from index 1 3
Last index of from index 2 3
Last index of from index 3 3
Last index of from index 4 3
Last index of from index 5 -1
Last index of from index -1 3
Last index of from index -2 3
Last index of from index -3 3
Last index of from index -4 3
Last index of from index -5 3

Can someone explain how positive and negative index work?

答案1

得分: 1

不过 indexOf(String, int) 方法没有明确说明,但 indexOf(int, int) 表现相同(重点是):

> 对于 fromIndex 的值没有限制。如果它是负数,其效果与它为零时相同:将搜索整个字符串。 如果它大于此字符串的长度,其效果与它等于此字符串的长度时相同:返回 -1。

(请注意,您的文本提到了 lastIndexOf,但代码是 indexOflastIndexOf 的逻辑相反:在任何负索引之后找到整个字符串,但在负索引之前不会找到任何部分字符串,因此使用负的起始点进行 lastIndexOf 永远不会找到任何内容。)

英文:

It is not spelled out for indexOf(String, int), but indexOf(int, int) behaves the same way (emphasis mine):

> There is no restriction on the value of fromIndex. If it is negative, it has the same effect as if it were zero: this entire string may be searched. If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned.

(Note that your text talks about lastIndexOf, but the code is indexOf. The logic for lastIndexOf is reversed: whereas the whole string is found after any negative index, none of the string is in front of a negative index, so lastIndexOf with a negative starting point will never find anything.)

huangapple
  • 本文由 发表于 2020年7月30日 11:29:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63165761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定