Java-不获取;StringIndexOutOfBoundsException“;如期
我有以下代码Java-不获取;StringIndexOutOfBoundsException“;如期,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我有以下代码 public class test{ public static void main(String[] args){ String one = "x"; if(one.charAt(one.indexOf('x')+1)== 'p'){ System.out.println("test"); } } } 这将导致以下错误: 线程“main”java.lang.StringIndexOutOfB
public class test{
public static void main(String[] args){
String one = "x";
if(one.charAt(one.indexOf('x')+1)== 'p'){
System.out.println("test");
}
}
}
这将导致以下错误:
线程“main”java.lang.StringIndexOutOfBoundsException中出现异常:字符串索引超出范围:1
位于java.lang.String.charAt(String.java:658)
at test.main(test.java:4)
我预计会发生这种情况,我的理解是,这是因为字符串只有1个索引(0),所以不可能在索引1处找到字符
one.charAt(one.indexOf('x')+1)== 'p'
如果我的理解是正确的,我无法理解为什么这个其他程序没有同样的问题
class XClass{
boolean doubleX(String str){
boolean is = false;
int indexes = str.length()-1;
if(str.indexOf('x')== indexes){
is = false;
}else if(str.charAt(str.indexOf('x')+1)=='x'){//same code as the program above
is = true;
}
return is;
}
}
public class ImplementXClass{
public static void main(String[] args){
XClass Xmen = new XClass();
boolean result = Xmen.doubleX("x");
System.out.println(result);
}
}
即使方法参数是带有一个索引(“x”)的字符串,此程序也能成功编译
- 如果没有索引1,程序如何运行这一行str.charAt(str.indexOf('x')+1)='x'
- 它不应该作为第一个程序编译失败吗
- 我错过了什么李>
if
条件
int indexes = str.length()-1; // length is 1, 1-1 is 0.
if(str.indexOf('x')== indexes){ // str.indexOf('x') == 0
is = false; // <-- hits this.
int index=str.length()-1;//长度为1,1-1为0。
如果(str.indexOf('x')==索引){//str.indexOf('x')==0
is=false;//因为您的一个字符串通过了第一个if
条件
int indexes = str.length()-1; // length is 1, 1-1 is 0.
if(str.indexOf('x')== indexes){ // str.indexOf('x') == 0
is = false; // <-- hits this.
int index=str.length()-1;//长度为1,1-1为0。
如果(str.indexOf('x')==索引){//str.indexOf('x')==0
is=false;//因为您的一个字符串通过了第一个if
条件
int indexes = str.length()-1; // length is 1, 1-1 is 0.
if(str.indexOf('x')== indexes){ // str.indexOf('x') == 0
is = false; // <-- hits this.
int index=str.length()-1;//长度为1,1-1为0。
如果(str.indexOf('x')==索引){//str.indexOf('x')==0
is=false;//因为您的一个字符串通过了第一个if
条件
int indexes = str.length()-1; // length is 1, 1-1 is 0.
if(str.indexOf('x')== indexes){ // str.indexOf('x') == 0
is = false; // <-- hits this.
int index=str.length()-1;//长度为1,1-1为0。
如果(str.indexOf('x')==索引){//str.indexOf('x')==0
is=false;//如果str
是“x”
,则如果甚至没有运行,则else
if
条件
if(str.indexOf('x') == indexes)
是true
,因为str.indexOf('x')
返回0
,index
也是0
-str.length()-1
因此,is
被设置为false
,如果
条件从未被评估,则设置为else。因此,不会发生索引自动边界异常
。如果str
为“x”
,则如果
未运行,则设置为else
//str = "x"
int indexes = str.length()-1; //indexes = 0;
if(str.indexOf('x')== indexes){ //evals to true, because the index of x is 0 in string "x"
is = false;
} else {
//... never executes because above evaluated to true
if
条件
if(str.indexOf('x') == indexes)
是true
,因为str.indexOf('x')
返回0
,index
也是0
-str.length()-1
因此,is
被设置为false
,如果
条件从未被评估,则设置为else。因此,不会发生索引自动边界异常
。如果str
为“x”
,则如果
未运行,则设置为else
//str = "x"
int indexes = str.length()-1; //indexes = 0;
if(str.indexOf('x')== indexes){ //evals to true, because the index of x is 0 in string "x"
is = false;
} else {
//... never executes because above evaluated to true
if
条件
if(str.indexOf('x') == indexes)
是true
,因为str.indexOf('x')
返回0
,index
也是0
-str.length()-1
因此,is
被设置为false
,如果
条件从未被评估,则设置为else。因此,不会发生索引自动边界异常
。如果str
为“x”
,则如果
未运行,则设置为else
//str = "x"
int indexes = str.length()-1; //indexes = 0;
if(str.indexOf('x')== indexes){ //evals to true, because the index of x is 0 in string "x"
is = false;
} else {
//... never executes because above evaluated to true
if
条件
if(str.indexOf('x') == indexes)
是true
,因为str.indexOf('x')
返回0
,index
也是0
-str.length()-1
因此,is
被设置为false
,如果
条件从未被计算,则为else。因此,不会发生索引自动边界异常
。因为该代码从未被执行。并且在第一个程序中没有编译错误。因为该代码从未被执行。在第一个程序中也没有编译错误程序。因为该代码从未被执行。并且在第一个程序中没有编译错误。因为该代码从未被执行。并且在第一个程序中没有编译错误。
//str = "x"
int indexes = str.length()-1; //indexes = 0;
if(str.indexOf('x')== indexes){ //evals to true, because the index of x is 0 in string "x"
is = false;
} else {
//... never executes because above evaluated to true