【问题标题】:Shift one rows in a 2D Array in Java在Java中的二维数组中移动一行
【发布时间】:2014-02-16 17:50:02
【问题描述】:

我正在尝试为我的计算机科学实验室制作一款名为 Numbrosia 的游戏。它必须是 5x5 板,但是当我尝试将行中的值向右或向左移动时,我的一些列消失了。

数组的值为

 1 -2  1  0  0
-1  0  4  2  0
 0 -4  1 -1  0
 0  1 -1 -1 -2
 0 -3  1 -1  0

但我得到了

0 0 1 -2 
0 2 4 0 
0 -1 1 -4 
0 -1 -1 1 
0 -1 1 -3 

我将把我的一段代码移到右边

public public static void RowRight(int i, int[][] array){
   for(int row = 0; row < array.length; row++){
       int temp = array[i][array.length-1];
       for(int col = array.length-2; col > 0; col--){
           array [row][col+1] = array[row][col];
           System.out.print(array[row][col] + " ");
       }
       System.out.println("");
   }
}      

我等待某人的回应谢谢你

---------更新------------------

我已成功打印了 5 列,但出现了一个新问题。如果我将行中的值向左或向右移动,最后一个数字(左移时是第一个数字)或第一个数字(右移时是最后一个数字)旁边的位置具有与第一个或最后一个完全相同的数字。 我会把输出放出来

数组值又是

 1 -2  1  0  0
-1  0  4  2  0
 0 -4  1 -1  0
 0  1 -1 -1 -2
 0 -3  1 -1  0

例如,我需要让第一行向左移动,所以它应该打印

-2  1  0  0  1
-1  0  4  2  0
 0 -4  1 -1  0
 0  1 -1 -1 -2
 0 -3  1 -1  0

但我得到了这个

-2  1  0  1  1
-1  0  4  2  0
 0 -4  1 -1  0
 0  1 -1 -1 -2
 0 -3  1 -1  0

我该如何解决这个问题?

【问题讨论】:

  • 您通常将行向右或向上移动,您的意思是向左/向右移动它们吗?
  • @peter.petrov - 我认为这意味着在一行内左/右移动值。
  • 如果只想移动一行,则不需要两个嵌套循环。请参阅我的更新答案。

标签: java arrays 2d row shift


【解决方案1】:

您可以按如下方式将第 #i 行向右/向左移动。

import java.util.Arrays;

public class Test037 {

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

        System.out.println(Arrays.deepToString(x));

        shiftRight(0, x);
        System.out.println(Arrays.deepToString(x));

        shiftLeft(0, x);
        System.out.println(Arrays.deepToString(x));

        shiftLeft(0, x);
        System.out.println(Arrays.deepToString(x));
    }

    /**
     * Shifts row #i one position to the right.
     */
    public static void shiftRight(int i, int[][] array) {
        int m = array[i].length;
        int temp = array[i][m-1];
        for (int k=m-1; k>=1; k--){
            array[i][k] = array[i][k-1];
        }
        array[i][0] = temp;
    }

    /**
     * Shifts row #i one position to the left.
     */
    public static void shiftLeft(int i, int[][] array) {
        int m = array[i].length;
        int temp = array[i][0];
        for (int k=0; k<m-1; k++){
            array[i][k] = array[i][k+1];
        }
        array[i][m-1] = temp;
    }

}

【讨论】:

【解决方案2】:

你没有对temp 做任何事情,所以它的价值会丢失。你可能想要这样的东西:

for(int row = 0; row < array.length; row++){
   int temp = array[i][array.length-1];
   System.out.print(temp + " ");
   for(int col = array.length-2; col >= 0; col--){
       array [row][col+1] = array[row][col];
       System.out.print(array[row][col] + " ");
   }
   array[row][0] = temp;
   System.out.println("");
}

【讨论】:

  • 对不起,这是我的一个错误,我把它改成里面有 temp,仍然给我同样的结果。
  • @user2958626 - 我将循环终止条件从col &gt; 0 更改为col &gt;= 0。看看这是否有什么不同。如果没有,请编辑您的内容以显示一个特定示例(带有值和输出),显示出了什么问题。
【解决方案3】:

这段代码可以解决你的问题。

private static void RowRight(int shiftRow, int[][] array){
    if(array.length != 0 && array[0].length != 0){
        // to check for empty arrays

        int temp = array[shiftRow][array[shiftRow].length - 1];

        for(int col = 1; col < array[shiftRow].length; col++){
            array[shiftRow][col] = array[shiftRow][col - 1]; 
        }

        array[shiftRow][0] = temp;
    }

    traverse(array);
}      

private static void traverse(int a[][]) {
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < a[i].length; j++) {
            System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多