Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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,如何将二维数组从索引位置移动 int[][] x = { { 1, 2, 3, 4, 5, 6, 7 }, { 1, 2, 3, 4, 5, 6, 7 }, { 1, 2, 3, 4, 5, 6, 7 }, { 1, 2, 3, 4, 5, 6, 7 } }; index = 3 int[][] y = { { 5, 6, 7, 1, 2, 3, 4 },

如何将二维数组从索引位置移动

int[][] x = 
    {
        { 1, 2, 3, 4, 5, 6, 7 },
        { 1, 2, 3, 4, 5, 6, 7 },
        { 1, 2, 3, 4, 5, 6, 7 },
        { 1, 2, 3, 4, 5, 6, 7 }
    };

index = 3

int[][] y = 
        {
            { 5, 6, 7, 1, 2, 3, 4 },
            { 5, 6, 7, 1, 2, 3, 4 },
            { 5, 6, 7, 1, 2, 3, 4 },
            { 5, 6, 7, 1, 2, 3, 4 }
        };

有什么想法吗?谢谢

这应该可以满足您的要求:

int[][] x = { { 1, 2, 3, 4, 5, 6, 7 }, 
              { 1, 2, 3, 4, 5, 6, 7 },
              { 1, 2, 3, 4, 5, 6, 7 },
              { 1, 2, 3, 4, 5, 6, 7 } };

int index = 3;

// Create 2D array of matching size
int[][] y = new int[x.length][x[0].length];

// Loop through each row of x
for (int r = 0; r < x.length; r++) {
    // Loop through each column of x[r][]
    for (int c = 0; c < x[0].length; c++) {
        // Put x's value in y, shifting to the right by index.
        // See comment outside of code regarding %
        y[r][(c + index) % x[0].length] = x[r][c];
    }
}

// Print out y to see if it worked
for (int r = 0; r < y.length; r++) {
    for (int c = 0; c < y[0].length; c++) {
        System.out.print(y[r][c] + " ");
    }
    System.out.println();
}
int[]x={{1,2,3,4,5,6,7},
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 } };
int指数=3;
//创建大小匹配的二维数组
int[][]y=新的int[x.length][x[0].length];
//循环遍历x的每一行
对于(int r=0;r

这里的键是
%x[0]。长度
y[r][(c+索引)%x[0]。长度]=x[r][c]
c+索引将列向右移动。但是,
%x[0].length会在必要时将列环绕到行的开头。

这里有一种方法,使用番石榴和Java 8:

Arrays.stream(x)
        .map(Ints::asList)
        .forEach(list -> Collections.rotate(list, index));
或Java 7版本:

for (int[] array : x) {
    Collections.rotate(Ints.asList(array), index);
}

这个程序使用一种逻辑,在这种逻辑中,我们通过部分反转数组来旋转数组。首先,我们将反转数组到索引位置,然后反转剩余的数组(索引+1到数组的最后一个元素)。 完成以上两个步骤后,我们再次调用reverse函数,但这次调用的是整个数组,它为我们提供了所需的输出

下面是有助于理解上述逻辑的代码

public class ShiftTwoDArray {

    public static void main(String[] args) {
        int[][] x = {   { 1, 2, 3, 4, 5, 6, 7 }, 
                        { 1, 2, 3, 4, 5, 6, 7 }, 
                        { 1, 2, 3, 4, 5, 6, 7 },
                        { 1, 2, 3, 4, 5, 6, 7 } 
                    };

        int index = 3;
        int i, j;
        // System.out.println(x.length);
        System.out.println("Before");
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                System.out.print(x[i][j] + "  ");
            }
            System.out.println();
        }
        rotate(x, index);

        System.out.println("\nAfter");
        for (i = 0; i < x.length; i++) {
            for (j = 0; j < x[i].length; j++) {
                System.out.print(x[i][j] + "  ");
            }
            System.out.println();
        }
    }

    /**
     * @param x
     * @param index
     * calls rotateUtil on each row
     */
    private static void rotate(int[][] x, int index) {
        for (int i = 0; i < x.length; i++) {
            rotateUtil(x[i], index);
        }
    }

    /**
     * @param x
     * @param index
     * reverse array in parts and then reverse whole array
     */
    private static void rotateUtil(int[] x, int index) {
        reverse(x, 0, index);
        reverse(x, index + 1, x.length - 1);
        reverse(x, 0, x.length - 1);
    }

    /**
     * @param x
     * @param start
     * @param end
     * reverse an array
     */
    private static void reverse(int[] x, int start, int end) {
        int temp = 0;
        while (start < end) {
            temp = x[start];
            x[start] = x[end];
            x[end] = temp;
            start++;
            end--;
        }
    }
}
公共类ShiftTwoDArray{
公共静态void main(字符串[]args){
int[]x={{1,2,3,4,5,6,7},
{ 1, 2, 3, 4, 5, 6, 7 }, 
{ 1, 2, 3, 4, 5, 6, 7 },
{ 1, 2, 3, 4, 5, 6, 7 } 
};
int指数=3;
int i,j;
//系统输出打印Ln(x.长度);
系统输出打印项次(“之前”);
对于(i=0;i
有什么想法吗?
-Java代码会把一些想法写下来-甚至写在纸上你好,欢迎来到stack overflow。请通读这一部分。这不是社区期望的问题。谢谢,我是StackOverflow的新手@IshitaSinhaTake创建一个包含3个元素的一维数组,并将其移动1。然后再进一步解决你的问题。谢谢@mook,我知道了。如果我更改了列索引,那么该列应该从最后一列开始。@RehanKhan没问题,很高兴我能提供帮助。在未来,最好展示一些您尝试过的例子,但这次我顺其自然:)@RehanKhan我不确定您对更改列索引的看法……只需更改
索引
,它将更改元素旋转的距离。谢谢@mook,我的意思是,如果我更改了列索引,那么该索引的所有列元素都应该从最后一个开始。@RehanKhan我仍然不确定你在说什么,但只要你弄明白了,就可以了。你有什么理由不接受我的答案而不投票吗?@mook两方面都错了。@mook@mook答案中肯定有。如果您碰巧使用了这个库(和许多人一样),我认为这是一个非常优雅的解决方案。@mook您可能想在字典中查找“simple”。@mook:代码有点长,但并不复杂。为了简单起见,提供了不同的功能,以便可以分别解释逻辑。@Rehan:我很高兴,这有帮助。:)