java数组索引找不到异常

java数组索引找不到异常,java,arrays,Java,Arrays,我正在学习java,在迭代数组时获得了ArrayIndexOutOfBoundsException。我在Stringbuffer中添加数字,用逗号分隔并用逗号分割,这是我的逻辑 StringBuffer sb = new StringBuffer(); for(int jj = 0;jj<2;jj++){ sb.append(""+jj); sb.append(","); } String arr[] = sb.toString().split(","); int len =

我正在学习java,在迭代数组时获得了ArrayIndexOutOfBoundsException。我在Stringbuffer中添加数字,用逗号分隔并用逗号分割,这是我的逻辑

StringBuffer sb = new StringBuffer();
for(int jj = 0;jj<2;jj++){
   sb.append(""+jj);
   sb.append(",");
}

String arr[] = sb.toString().split(",");
int len = arr.length;
System.out.println("len "+len); // it print len 2
for(int i=0;i<len;i++){
 System.out.println(arr[i]); // but I get index out of found exception here with index 2....
}
StringBuffer sb=new StringBuffer();

对于(int jj=0;jj我已经尝试了您的代码,但没有得到任何异常,我的示例中的输出是:

透镜2

0


1

java中的数组索引从0开始。例如,如果数组的大小为10,则数组的索引范围为0到9

阵列arr的大小为2。 您可以访问arr[0],arr[1],但不能访问arr[2]


PS:我想你的意思是越界,而不是越界。

它应该可以正常工作。没有索引出异常。Make
s
capital for
System
我已经运行了你的代码,它没有给出任何异常。它给出了这个结果len 2 0 1该代码工作正常,你能发布不工作的确切代码吗?除了它正在打印-len 2 0 1“找不到异常”是最好的异常。