代码可以编译,但在其运行程序中出现越界异常。 for循环有问题吗?
public class CountPairs
{
public static int pairCounter( String str )
{
//count pairs and aaa counts as 2
int count = 0;
int stringLength = str.length();
String string = str;
for( int i = 1; i <= stringLength; i++ )
{
char first = string.charAt( 0 );
if( first == string.charAt( i ) )
{
count++;
}
string = string.substring( i );
}
return count;
}
}
答案 0 :(得分:1)
您应该替换下面的行:
for( int i = 1; i <= stringLength; i++ )
带线:
for( int i = 0; i < stringLength; i++ )
您应该只以索引 i = 0
开始循环遍历您的字符串
答案 1 :(得分:1)
首先
String
基本上是对 char[] array
的包装。
第二
Java 中的 array
索引以零开头。负索引在 Java 中无效。
第三
Java 将抛出 ArrayIndexOutOfBoundException
,如果您尝试访问带有 无效索引 的 Array
,这可能意味着“负索引”、“索引大于或等于数组的长度”在 Java 中。
您的问题代码:
public class CountPairs
{
public static int pairCounter( String str )
{
//count pairs and aaa counts as 2
int count = 0;
int stringLength = str.length();
String string = str;
for( int i = 1; i <= stringLength; i++ )
{
char first = string.charAt( 0 );
if( first == string.charAt( i ) )
{
count++;
}
string = string.substring( i );
}
return count;
}
}
可能的解决方案:
public class CountPairs
{
public static int pairCounter( String str )
{
//count pairs and aaa counts as 2
int count = 0;
int stringLength = str.length();
String string = str;
for( int i = 0; i < stringLength; i++ )
{
char first = string.charAt( 0 );
if( first == string.charAt( i ) )
{
count++;
}
string = string.substring( i );
}
return count;
}
}
希望这可以解决您的问题。
点赞如果是。
继续学习 Java!
答案 2 :(得分:0)
由于String
中第一个字符的索引为0,最后一个字符的索引为myString.length() - 1
,myString.charAt(myString.length())
抛出一个StringOutOfBoundsException
。
要修复您的代码,请将循环标头更改为 for (int i = 0;i < myString. length();i++)
答案 3 :(得分:0)
这个答案分为三个部分:
这是编译并返回值的代码版本。但如果这是您预期的回报值,我不知道。将您的代码与它应该实现的实际任务(详细)一起使用会很棒。
我改变的是:
<=
) 更改为lower-than (<
),因为字符串的最大索引等于其长度减一stringLength
的重新计算,因为 string
及其每次迭代中的长度变化作为 string = string.substring( i )
的原因public class CountPairs
{
public static int pairCounter( String str )
{
//count pairs and aaa counts as 2
int count = 0;
int stringLength = str.length();
String string = str;
// change "<=" to "<" because
// the index of the last character of a string
// equals its length MINUS 1
for( int i = 1; i < stringLength; i++ )
{
char first = string.charAt( 0 );
if( first == string.charAt( i ) )
{
count++;
}
// Here you reassign "string" to be a substring of itself
// effectively changing its length and index positions completely
string = string.substring( i );
// so you have to recalculate the Length of "string"
// which changes in each iteration!
stringLength = string.length();
}
return count;
}
}
但除此之外,我认为您真正想要的是大小为 2 的滑动窗口,如果此窗口中的字符相等,则增加计数器。这可以类似地实现,但无需调用 substring
:
public class CountPairs
{
public static int pairCounter( String str )
{
//count pairs and aaa counts as 2
int count = 0;
int stringLength = str.length();
String string = str;
for( int i = 0; i < stringLength - 1; i++ )
{
char first = string.charAt( i );
if( first == string.charAt( i + 1 ) )
{
count++;
}
}
return count;
}
}
以下是一些伪语法,显示变量及其值如何随您最初定义的函数而变化。
还有一些帮助显示索引和所涉及字符串的位置。 string
的相关索引由下面的 ^
标记,并附有下面另一行的索引位置
stringLength = 6
string = "aabccd"
// ...
// here the first iteration of the loop starts
i = 1
first = "aabccd".charAt( 0 ) = "a"
^
012345
second = "aabccd".charAt(i = 1) = "a"
^
012345
// first equals second so increment counter
first == second => count++
string = "aabccd".substring( i = 1) = "abccd"
^^^^^
012345
-------------
// second for loop iteration, i is incremented to 2, string is "abccd"
i = 2
first = "abccd".charAt( 0 ) = "a"
^
01234
second = "abccd".charAt(i = 2) = "c"
^
01234
first != second
string = "abccd".substring( i = 2) = "ccd"
^^^
01234
-------------
// third for loop iteration, i is incremented to 3, string is "ccd"
i = 3
first = "ccd".charAt( 0 ) = "a"
^
012
// Here already an index out of bounds exception is thrown!!
// because 3 is larger than the highest possible index position 2
second = "ccd" .charAt(i = 3) = "c"
^
012