仅使用一个分号在Java中旋转int数组 [英] Rotating an int Array in Java using only one semicolon

查看:75
本文介绍了仅使用一个分号在Java中旋转int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Java做一堆自愿任务,其中涉及使用基本数组和方法.我一直表现不错,直到遇到严重的障碍.

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.

最初的任务是创建方法 rotateByN ,该方法应采用一个int数组并将其旋转N步(并影响输入数组,而不返回新数组).很简单:

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.

更令人沮丧的是,我们是否可以仅调用一个先前创建的函数 rotate (它执行相同的操作,但始终只执行一次)N次,但仍未得到澄清,for循环将需要更多我们没有的分号.

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.

经过更多的询问后,发现它变得无聊,就像一次又一次地调用 rotate 方法一样.因为(我认为)只能用更多的分号代替,所以也许不算在内.

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.

推荐答案

您可以使用

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);
}


或者没有 streams ,您可以使用


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);
}

这篇关于仅使用一个分号在Java中旋转int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆