简体   繁体   English

为什么没有越界例外?

[英]Why no out of bounds exception?

I am looking at a problem which asks to return the number of the positions where two given strings contain the same length 2 substring. 我正在看一个问题,要求返回两个给定字符串包含相同长度2子字符串的位置数。 So "xxcaazz" and "xxbaaz" yields 3, since the "xx", "aa", and "az" substrings appear in the same place in both strings. 因此,“ xxcaazz”和“ xxbaaz”产生3,因为“ xx”,“ aa”和“ az”子字符串在两个字符串中的相同位置出现。 The solution is said to be the following: 据说解决方法如下:

public int stringMatch(String a, String b) {
  // Figure which string is shorter.
  int len = Math.min(a.length(), b.length());
  int count = 0;

  // Look at both substrings starting at i
  for (int i=0; i<len-1; i++) {
    String aSub = a.substring(i, i+2);
    String bSub = b.substring(i, i+2);
    if (aSub.equals(bSub)) {  // Use .equals() with strings
      count++;
    }
  }

  return count;
}

I don't understand why there is no out of bounds exception for this solution. 我不明白为什么这个解决方案没有例外。 If for example, there are two string inputted with length 6 and 7 respectively, in the final iteration of the for loop, i=5. 例如,如果有两个字符串分别以长度6和7输入,则在for循环的最终迭代中,i = 5。 But then for the substring of the smaller string, the parameters given would be (5,7) even though the final index of the string is 5. In previous problems I seem to have produced an out of bounds exception in a similar circumstance. 但是,对于较小的字符串的子字符串,即使字符串的最终索引为5,给出的参数也将是(5,7)。在以前的问题中,在类似情况下,我似乎产生了超出范围的异常。 Why not here? 为什么不在这里? All help greatly appreciated. 所有帮助,不胜感激。

If we suppose that you're coding in Java, in the method substring(int beginIndex, int endIndex) , endIndex is exclusive. 如果我们假设您使用Java进行编码,则在substring(int beginIndex, int endIndex)endIndex是排他的。

From http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int- : http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring-int-int-

Parameters: 参数:

beginIndex - the beginning index, inclusive. beginIndex起始索引(含)。

endIndex - the ending index, exclusive. endIndex结束索引(不包括)。

So when you're calling your last indent, i is equals to 4 because i<len-1 in the for condition; 因此,当您调用最后一个缩进时, i等于4,因为在for条件下i<len-1 ; so: 所以:

String bSub = b.substring(i, i+2);

=> b.substring(4, 6) => xxba az => b.substring(4,6)=> xxba az

If you want an StringIndexOutOfBoundsException , remove -1 in your for condition. 如果需要StringIndexOutOfBoundsException ,则将for条件中的-1删除。

As you were mentioning " In the final iteration of the for loop, i=5 ". 正如您提到的“ 在for循环的最终迭代中,i = 5 ”。 In the 5th iteration the i = 4 you are starting from 0th index. 在第5次迭代中,i = 4从第0个索引开始。 So the output is 所以输出是

0> xx == xx
1> xc == xb
2> ca == ba
3> aa == aa
4> az == az 

For the substring function the second parameter in exclusive so substring(4,6) never tries to read the index 6. Thus, the program does not result IndexOutOfBoundsException . 对于substring函数,第二个参数不包含在内,因此substring(4,6)永远不会尝试读取索引6。因此,程序不会导致IndexOutOfBoundsException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM