我正在尝试拆分一个数组,将一部分存储在一个数组中,另一部分存储在另一个数组中。然后我尝试翻转 2 并将它们存储在一个新数组中。这就是我所拥有的
public int[] flipArray(){
int value = 3;
int[] temp1 = new int[value];
int[] temp2 = new int[(a1.length-1) - (value+1)];
int[] flipped = new int[temp1.length+temp2.length];
System.arraycopy(a1, 0, temp1, 0, value);
System.arraycopy(a1, value+1, temp2, 0, a1.length-1);
System.arraycopy(temp2, 0, flipped, 0, temp2.length);
System.arraycopy(temp1, 0, flipped, temp2.length, temp1.length);
return flipped;
}
private int[]a1={1,2,3,4,5,6,7,8,9,10};
最佳答案
当你想要访问 [0, length - 1] 范围之外的数组元素时,你会得到 ArrayIndexOutOfBoundsException;
如果您使用调试器,或者,您可以自己找到问题 在每次调用 System.arraycopy 之前放置 System.out.println(text),在其中输出源数组和目标数组的数组长度以及要复制的元素数
关于java.lang.ArrayIndexOutOfBoundsException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13674382/