Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-不获取;StringIndexOutOfBoundsException“;如期_Java_Indexoutofboundsexception - Fatal编程技术网

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