Java 左移多维数组

Java 左移多维数组,java,multidimensional-array,Java,Multidimensional Array,我有一个多维数组: int[][] arrMulti = new int [3][3]; 我编写了一个简单的循环,以网格形式显示其中的数据: 123 456 789 我现在需要做的是将所有的东西向左移动1,并用零替换网格右侧的空白,如下所示: 230 560 890 理想情况下,我希望该方法能够移动任何大小的多维数组,例如 int[][] arrM = new int [28][28] or [98][98] 有人能帮我吗 谢谢 这是我目前的代码: package testingarry

我有一个多维数组:

int[][] arrMulti = new int [3][3];
我编写了一个简单的循环,以网格形式显示其中的数据:

123
456
789
我现在需要做的是将所有的东西向左移动1,并用零替换网格右侧的空白,如下所示:

230
560
890
理想情况下,我希望该方法能够移动任何大小的多维数组,例如

int[][] arrM = new int [28][28] or [98][98]
有人能帮我吗

谢谢

这是我目前的代码:

package testingarrymove;


public class TestingArryMove {

//Start of shift array method
public static int[][] shiftArray (int arrShiftLeft[][] ) {

int from = 1;
int   to = 0;

for (int i = 0; i < arrShiftLeft.length; i++) {
    for (int j = 0; j < arrShiftLeft[0].length; j++) {    


    // move 1 to 0, 2 to 1, 3 to 2, 4 to 3, 5 to 4 ............
    System.arraycopy(arrShiftLeft, 1, arrShiftLeft, 0, arrShiftLeft.length - 1);

    from++;
    to++;

        return arrShiftLeft;
    }
}
    return null;   
} // end shiftArray

public static void main(String[] args) {

int [][] arrMultiDim = new int [3][3];

arrMultiDim [0][0] = 1;
arrMultiDim [0][1] = 2;
arrMultiDim [0][2] = 3;
arrMultiDim [1][0] = 4;
arrMultiDim [1][1] = 5;
arrMultiDim [1][2] = 6;
arrMultiDim [2][0] = 7;
arrMultiDim [2][1] = 8;
arrMultiDim [2][2] = 9;

// outputs original array
System.out.print("Original Array: ");
    //loops thought the rows of the array
    for (int i = 0; i < arrMultiDim.length; i++) {

       System.out.println();
        //loops thought the columns of the array
        for (int j = 0; j < arrMultiDim[0].length; j++) {    
            System.out.print(" "+arrMultiDim[i][j]);

        }

    }
    System.out.println();

    System.out.println("Shifted Array: "); 
    //this should copy just the row to another array by calling the shiftedArray
    shiftArray(arrMultiDim);
    //outputs the shifted array
    System.out.println(arrMultiDim);
}

} // end class

乍一看,我认为双端队列数据结构在这种情况下非常方便。您可以声明一个包含3行的队列数组,并使用队列函数左右移动。例如,对于右班,您可以将一个项目出列并将0放入队列,对于左班,您可以将另一端的0放入队列并将其放入队列。您可能首先要查找并最终实现这些函数,或者甚至使用现有库来使用(代表[d]ouble[e]和queue)数据结构。通过这种方式,数组的大小对于执行您想要的操作来说并不重要,但是您必须稍微更改您提到的数据类型(3x3数组)。

public static void shift(int[]]arr,int offs){
if(offs arr[0]。长度?arr[0]。长度:offs;
对于(int[]行:arr){
如果(关闭<阵列长度)
System.arraycopy(行,关闭,行,0,行长度-关闭);
数组.填充(行,行.长度-offs,arr.length,0);
}
}
Original Array: 
1 2 3
4 5 6
7 8 9
Shifted Array: 
[[I@ece88d2
public static void shift(int[][] arr, int offs) {
    if (offs <= 0)
        return;

    offs = offs > arr[0].length ? arr[0].length : offs;

    for (int[] row : arr) {
        if (offs < arr.length)
            System.arraycopy(row, offs, row, 0, row.length - offs);
        Arrays.fill(row, row.length - offs, arr.length, 0);
    }

}