我试图拆分一个数组,将一个部分存储在一个数组中,另一部分存储在另一个数组中。然后即时通讯试图翻转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 - java.lang.ArrayIndexOutOfBoundsException错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13674382/

10-11 22:28
查看更多