Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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 完成交换整数数组的前半部分和后半部分的方法_Java_Arrays - Fatal编程技术网

Java 完成交换整数数组的前半部分和后半部分的方法

Java 完成交换整数数组的前半部分和后半部分的方法,java,arrays,Java,Arrays,每当我试图运行代码时,总是会出现越界错误。有人知道它出了什么问题吗?我似乎不明白 public class Swapper{ /** This method swaps the first and second half of the given array. @param values an array */ public void swapFirstAndSecondHalf(int[] values) { // your wor

每当我试图运行代码时,总是会出现越界错误。有人知道它出了什么问题吗?我似乎不明白

public class Swapper{

    /**
    This method swaps the first and second half of the given array.
    @param values an array
     */

    public void swapFirstAndSecondHalf(int[] values) {
        // your work here

        int[] first = new int[values.length/2];
        int[] second = new int[values.length/2];
        for(int i = 0; i < values.length / 2; i++) {
            second[i] = values[i];
        }
        for (int j = values.length / 2; j < values.length; j++) {
            first[j] = values[j];
        }
        for(int k = 0; k < values.length / 2; k++) {
            values[k] = first[k];
        }
        for(int l = values.length / 2; l < values.length; l++) {
            values[l] = second[l];
        }
    }

    // This method is used to check your work
    public int[] check(int[] values) {
        swapFirstAndSecondHalf(values);
        return values;
    }
}
因此索引[0..values.length/2-1]对第一个有效

因此,j的第一个值是values.length/2,它已经超出了界限

您需要练习调试、放置断点并在代码执行时跟踪代码。

您可以使用System.arraycopy而不是所有用于循环的选项

结果:

[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]
[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]
如果希望奇数数组的中间数成为下半部分的一部分,那么SwapFirstAndSecond半部分将如下所示:

public static int[] swapFirstAndSecondHalf(int[] values) {
    boolean evenSize = values.length % 2 == 0;
    int half = values.length / 2;
    int[] swapper = new int[values.length];
    System.arraycopy(values, half, swapper, 0, evenSize ? half : half + 1);
    System.arraycopy(values, 0, swapper, evenSize ? half : half + 1, half);
    return swapper;
}
结果:

[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]
[4, 5, 3, 1, 2]
[4, 5, 6, 1, 2, 3]

分配新阵列是对空间的浪费。只需在适当位置交换两半:

public static void swapFirstAndSecondHalf(int[] values) {
    final int len = values.length / 2;
    final int offset = values.length - len;
    for (int i = 0; i < len; i++) {
        int temp = values[i];
        values[i] = values[offset + i];
        values[offset + i] = temp;
    }
}

该代码允许奇数长度,并将不使用中心值。

能否提供一些导致AIOOBE的示例输入?请尝试打印出在每个for循环中修改的数组的字符串表示形式。
public static void swapFirstAndSecondHalf(int[] values) {
    final int len = values.length / 2;
    final int offset = values.length - len;
    for (int i = 0; i < len; i++) {
        int temp = values[i];
        values[i] = values[offset + i];
        values[offset + i] = temp;
    }
}