在te double for循环中,您迭代到数组的末尾(c1.length和c2.length),同时在循环中添加一个数字newc[newi+X],但由于您循环到末尾,您将用完所有位置,并且您将获得IndexOutOfBoundsException。。。
for (int i=0 ; i<c1.length ; i++) {
for (int j=0 ; j<c2.length ; j++) {
newc[newi]=c1[i];
newc[newi+1] = '*';
newc[newi+2] = c2[j];
newc[newi+3] = '+';
newi+=4;
}
}
更新:
额外解释:-)
如果你这样做:
public class ArrayExample {
public static void main(String[] args) {
String[] strings = new String[3];
System.out.println("strings.length = " + strings.length);
strings[4] = "foo";
}
}
输出将为:
strings.length = 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at nl.ivonet.ArrayExample.main(ArrayExample.java:26)
这是因为我试图在strings数组的索引4处赋值,但strings数组是用
new String[3]
将其固定为3。这就是问题所在,也是代码失败的原因。
数组与列表不同。数组是固定大小的,列表不是。