Rotating an int Array in Java using only one semicolon

一世执手 提交于 2020-12-26 12:06:40

问题


I'm doing a bunch of voluntaty tasks for Java, which cover working with basic arrays and methods. I've been doing quite well, until I hit a severe roadblock.

The initial task was to create a method rotateByN, which should take an int array and rotate it by N steps (and affect the input array, not return a new one). Simple enough:

public class Four {
    static void rotateByN(int[] in, int n) {
        for (int l = 1; l <= n; l++) {
            int tmp = in[0];
            for (int i = 1; i < in.length; i++) {
                in[i - 1] = in[i];
            }
            in[in.length - 1] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = { 2, 7, 6, 1, 0 };
        rotateByN(arr, 3);
        for (int r : arr) {
            System.out.println(r);
        }
    }
}

However, the follow up task was to reduce the amounts of semicolons used in the method body to only one. I've spent about an hour crawling through documentation of all sorts of possibilities, but nothing seemed to get me there.

The more frustrating part is that it wasn't clarified if we could just call a previously created function rotate (which does the same thing, but always exactly once) N times, but then again, a for loop would need more semicolons which we don't have.

I hate to be asking for something so bluntly, but I'm completely out of options, and any kind of input would be greatly appreciated.


After some more questioning, it turns out to be as boring as calling the rotate method over and over again. Since this can only (I think) be done with more semicolons in the for head, perhaps they aren't counted.


回答1:


You can use Arrays.stream(int[],int,int) method two times to get two streams with the specified ranges of array: near and far, then swap and flatten them to one stream - return new array. Then you can use System.arraycopy method to replace the contents of the old array with the contents of the new one. Long path with one semicolon:

static void rotateByN(int[] arr, int n) {
    System.arraycopy(Stream
            .of(Arrays.stream(arr, n, arr.length),
                    Arrays.stream(arr, 0, n))
            .flatMapToInt(Function.identity())
            .toArray(), 0, arr, 0, arr.length);
}
public static void main(String[] args) {
    int[] arr = {2, 7, 6, 1, 0};
    rotateByN(arr, 3);
    System.out.println(Arrays.toString(arr)); // [1, 0, 2, 7, 6]
}

Or you can use Arrays.copyOfRange(int[],int,int) method. The idea is the same:

static void rotateByN(int[] arr, int n) {
    System.arraycopy(Stream
            .of(Arrays.copyOfRange(arr, n, arr.length),
                    Arrays.copyOfRange(arr, 0, n))
            .flatMapToInt(Arrays::stream)
            .toArray(), 0, arr, 0, arr.length);
}

Or without streams you can use ArrayUtils.addAll(int[],int[]) method:

static void rotateByN(int[] arr, int n) {
    System.arraycopy(ArrayUtils.addAll(
            Arrays.copyOfRange(arr, n, arr.length),
            Arrays.copyOfRange(arr, 0, n)),
            0, arr, 0, arr.length);
}


来源:https://stackoverflow.com/questions/65060947/rotating-an-int-array-in-java-using-only-one-semicolon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!