尝试从包含字符和整数的字符串中创建新的字符串数组时获取 IndexOutOfBoundExceptions
Getting IndexOutOfBoundExceptions when trying to create a new string array out of strings that contain chars and integers
我在今天的一次采访中被问到以下问题,要求我们编写一个小程序来检查两个文本源是否实际上相同。
我得到了一个解决方案来检查两个字符串是否长度相同,否则,它不匹配。但是,我试图想出一个解决方案,首先创建一个 "filtered" 数组,即将 A2Le
转换为 A..Le
,然后将其与 2pL1
过滤后为 ..pL.
。我在那里失败得很惨。附上我的尝试:
int SLength = 0;
int TLength = 0;
String[] SParts = S.split("[^\d]+");
String[] TParts = T.split("[^\d]+");
for(int i = 0; i < SParts.length; i++) {
if(!SParts[i].equals("")){
SLength += Integer.parseInt(SParts[i]);
}
}
for(int i = 0; i < TParts.length; i++) {
if(!TParts[i].equals("")){
TLength += Integer.parseInt(TParts[i]);
}
}
for(int i = 0; i < S.length(); i++) {
if(!Character.isDigit(S.charAt(i))){
SLength += 1;
}
}
for(int i = 0; i < T.length(); i++) {
if(!Character.isDigit(T.charAt(i))){
TLength += 1;
}
}
if(TLength != SLength) {
return false;
}
// Convert String S from "A2Le" to "A..Le"
char[] sArray = S.toCharArray();
char[] sArrayFiltered = new char[SLength + 1];
int sIndex = 0;
for(int i = 0; i < sArray.length; i++) {
if(Character.isDigit(sArray[i])) {
sIndex += Character.getNumericValue(sArray[i]);
System.out.println("Digit Index: " + sIndex + ", Char: " + sArray[i] + ", Index: " + i);
} else {
sArrayFiltered[i] = sArray[sIndex];
sIndex++;
System.out.println("Char Index: " + sIndex + ", Char: " + sArray[i] + ", Index: " + i);
}
}
我一直这样 IndexOutOfBoundExceptions
:
Char Index: 1, Char: A, Index: 0
Digit Index: 3, Char: 2, Index: 1
Char Index: 4, Char: L, Index: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ReverseString.solution(ReverseString.java:192)
at ReverseString.main(ReverseString.java:210)
此示例使用了 A2Le
示例字符串。
我试图通过调试器单步执行我的代码,但这似乎并没有帮助我。
编辑:我知道 OutOfBoundException 的原因是什么 - 但不知道 为什么 它是在这个问题的上下文中引起的。我不是在问 为什么 我有一个 OutOfBound,而是 where。
sArray
的长度为 4
所以最后一个索引是 3
.
在下一行,sIndex
的值为4
,index
不存在,因此异常。
sArrayFiltered[i] = sArray[sIndex];
我认为逻辑上的缺陷是下面这行
sIndex += Character.getNumericValue(sArray[i]);
为什么要将数值添加到 sIndex
?
我在今天的一次采访中被问到以下问题,要求我们编写一个小程序来检查两个文本源是否实际上相同。
我得到了一个解决方案来检查两个字符串是否长度相同,否则,它不匹配。但是,我试图想出一个解决方案,首先创建一个 "filtered" 数组,即将 A2Le
转换为 A..Le
,然后将其与 2pL1
过滤后为 ..pL.
。我在那里失败得很惨。附上我的尝试:
int SLength = 0;
int TLength = 0;
String[] SParts = S.split("[^\d]+");
String[] TParts = T.split("[^\d]+");
for(int i = 0; i < SParts.length; i++) {
if(!SParts[i].equals("")){
SLength += Integer.parseInt(SParts[i]);
}
}
for(int i = 0; i < TParts.length; i++) {
if(!TParts[i].equals("")){
TLength += Integer.parseInt(TParts[i]);
}
}
for(int i = 0; i < S.length(); i++) {
if(!Character.isDigit(S.charAt(i))){
SLength += 1;
}
}
for(int i = 0; i < T.length(); i++) {
if(!Character.isDigit(T.charAt(i))){
TLength += 1;
}
}
if(TLength != SLength) {
return false;
}
// Convert String S from "A2Le" to "A..Le"
char[] sArray = S.toCharArray();
char[] sArrayFiltered = new char[SLength + 1];
int sIndex = 0;
for(int i = 0; i < sArray.length; i++) {
if(Character.isDigit(sArray[i])) {
sIndex += Character.getNumericValue(sArray[i]);
System.out.println("Digit Index: " + sIndex + ", Char: " + sArray[i] + ", Index: " + i);
} else {
sArrayFiltered[i] = sArray[sIndex];
sIndex++;
System.out.println("Char Index: " + sIndex + ", Char: " + sArray[i] + ", Index: " + i);
}
}
我一直这样 IndexOutOfBoundExceptions
:
Char Index: 1, Char: A, Index: 0
Digit Index: 3, Char: 2, Index: 1
Char Index: 4, Char: L, Index: 2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ReverseString.solution(ReverseString.java:192)
at ReverseString.main(ReverseString.java:210)
此示例使用了 A2Le
示例字符串。
我试图通过调试器单步执行我的代码,但这似乎并没有帮助我。
编辑:我知道 OutOfBoundException 的原因是什么 - 但不知道 为什么 它是在这个问题的上下文中引起的。我不是在问 为什么 我有一个 OutOfBound,而是 where。
sArray
的长度为 4
所以最后一个索引是 3
.
在下一行,sIndex
的值为4
,index
不存在,因此异常。
sArrayFiltered[i] = sArray[sIndex];
我认为逻辑上的缺陷是下面这行
sIndex += Character.getNumericValue(sArray[i]);
为什么要将数值添加到 sIndex
?