java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:,java,Java,我知道有人问过类似的问题,但他们都有相同的问题:在循环内部,他们有类似的问题 i <= aString.lenth() i在您呼叫 第一个参数应该是i,而不是currChar: replaced.setCharAt(i, ch); 您应该传递要将字符设置为的索引,而不是字符本身 currChar的int值可能高于导致异常的StringBuilder的长度,但如果不是,则会得到异常的奇怪输出,这更糟。在调用 第一个参数应该是i,而不是currChar: replaced.setCharA

我知道有人问过类似的问题,但他们都有相同的问题:在循环内部,他们有类似的问题

i <= aString.lenth()
i在您呼叫

第一个参数应该是
i
,而不是
currChar

replaced.setCharAt(i, ch);
您应该传递要将字符设置为的索引,而不是字符本身

currChar
int
值可能高于导致异常的
StringBuilder
的长度,但如果不是,则会得到异常的奇怪输出,这更糟。

在调用

第一个参数应该是
i
,而不是
currChar

replaced.setCharAt(i, ch);
您应该传递要将字符设置为的索引,而不是字符本身


currChar
int
值可能高于导致异常的
StringBuilder
的长度,但如果不是,则会得到异常的奇怪输出,更糟糕的是。

以下代码是AbstractStringBuilder类的setCharAt方法

public void setCharAt(int index, char ch) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    value[index] = ch;
}
public void setCharAt(int-index,char-ch){
如果((索引<0)| |(索引>=计数))
抛出新StringIndexOutOfBoundsException(索引);
值[指数]=ch;
}
如果将字符'a'传递给setCharAt方法,则它将在索引处从character隐式转换为int值97。
它使java.lang.StringIndexOutOfBoundsException产生了。谢谢:)

以下代码是AbstractStringBuilder类的setCharAt方法

public void setCharAt(int index, char ch) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    value[index] = ch;
}
public void setCharAt(int-index,char-ch){
如果((索引<0)| |(索引>=计数))
抛出新StringIndexOutOfBoundsException(索引);
值[指数]=ch;
}
如果将字符'a'传递给setCharAt方法,则它将在索引处从character隐式转换为int值97。
它使java.lang.StringIndexOutOfBoundsException产生了。谢谢:)

@JohnSmith错误出现在标题中-java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:哪一行给出了错误?请回答您的问题,并包括堆栈跟踪的相关部分。在哪次迭代中会发生这种情况?@LutzHorn包括在内-上面有一条注释。这就是错误消息。但它必须包括发生的行号。哪一行导致了错误?@JohnSmith错误出现在标题-java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:哪一行导致了错误?请回答您的问题,并包括堆栈跟踪的相关部分。在哪次迭代中会发生这种情况?@LutzHorn包括在内-上面有一条注释。这就是错误消息。但它必须包括发生的行号。哪一行导致了错误?非常感谢,现在有意义了。非常感谢,现在有意义了。
replaced.setCharAt(i, ch);
public void setCharAt(int index, char ch) {
    if ((index < 0) || (index >= count))
        throw new StringIndexOutOfBoundsException(index);
    value[index] = ch;
}