在一个通行证中移动所有元素

发布于 2025-02-02 01:57:28 字数 75 浏览 1 评论 0原文

我试图弄清楚如何在java中逐渐增加一个值,而无需使用第二个数组来存储值。有没有办法一次在内存中只有几个(1-3ish)的值并转移一切?

I'm trying to figure out how to do a value shift in an array by one value incrementally in Java, without using a second array to store values. Is there a way to just have a couple (1-3ish) of values in memory at a time and shift over everything?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2025-02-09 01:57:29

在单个通行证中移动所有内容实际上是气泡弹头的单个迭代。

,但是,如果它对您有用,我建议您在Java阵列周围包装器,您将存储其他参数 - > shift。通过这样的实现,您将在阵列读取/写操作中实现巨大的效率,而代码消费者将无法弄清楚差异。

问题在于

int pos = (index + shift) % n;

使用方法shift()在“实时”中计算位置/索引:下面:

public class Array {

    private int[] arr;
    int n;
    private int shift;

    public Array(int n) {
        this.n = n;
        this.arr = new int[n];
        shift = 0;              // no shift, so shift is zero
    }

    public void set(int index, int value) {
        if (index >= n) { throw new IllegalArgumentException();  }
        int pos = ( index + shift ) % n;
        arr[pos] = value;
    }

    public int get(int index) {
        if (index >= n) { throw new IllegalArgumentException();  }
        int pos = ( index + shift ) % n;
        return arr[pos];
    }

    public void shift(int places) {
        shift = ( shift + places ) % n;
    }

    public void print() {
        for (int index = 0; index < n; index++) {
            int pos = (index + shift) % n;
            System.out.print(arr[pos] +" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        Array array = new Array(4);
        array.set(0,0); array.set(1,1); array.set(2,2); array.set(3,3);
        array.print();
        // 0 1 2 3

        array.shift(1);
        array.print();
        // 1 2 3 0 
    }
}

To shift everything in a single pass is actually a single iteration of bubble-sort.

But, if it works for you, I would recommend the wrapper around java array where you will store additional parameter -> shift. With such an implementation, you would achieve great efficiency in array read/write operation and code consumers will not figure out the difference.

The thing is in calculation of the position/index in "real time"

int pos = (index + shift) % n;

Full code with method shift() is below:

public class Array {

    private int[] arr;
    int n;
    private int shift;

    public Array(int n) {
        this.n = n;
        this.arr = new int[n];
        shift = 0;              // no shift, so shift is zero
    }

    public void set(int index, int value) {
        if (index >= n) { throw new IllegalArgumentException();  }
        int pos = ( index + shift ) % n;
        arr[pos] = value;
    }

    public int get(int index) {
        if (index >= n) { throw new IllegalArgumentException();  }
        int pos = ( index + shift ) % n;
        return arr[pos];
    }

    public void shift(int places) {
        shift = ( shift + places ) % n;
    }

    public void print() {
        for (int index = 0; index < n; index++) {
            int pos = (index + shift) % n;
            System.out.print(arr[pos] +" ");
        }
        System.out.println();
    }

    public static void main(String[] args) {

        Array array = new Array(4);
        array.set(0,0); array.set(1,1); array.set(2,2); array.set(3,3);
        array.print();
        // 0 1 2 3

        array.shift(1);
        array.print();
        // 1 2 3 0 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文