Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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,我目前正在做一个小项目。我正在为L游戏编写一个简单的java程序。我需要编写一个方法来移动4x4数组的内容。此方法将采用(行、列)参数。内容也会随之移动 { 'x',' ',' ',' ' }, { 'x',' ',' ',' ' }, { 'x','x',' ',' ' }, { ' ',' ',' ',' ' } 移动(0,2) ---> 我不知道从哪里开始。我非常感谢在这方面给予的任何帮助。 非常感谢您的帮助。您的方法应该是这样的 char[][] array = new cha

我目前正在做一个小项目。我正在为L游戏编写一个简单的java程序。我需要编写一个方法来移动
4x4
数组的内容。此方法将采用
(行、列)
参数。内容也会随之移动

{ 'x',' ',' ',' ' }, 

{ 'x',' ',' ',' ' },

{ 'x','x',' ',' ' },

{ ' ',' ',' ',' ' }
移动(0,2) --->

我不知道从哪里开始。我非常感谢在这方面给予的任何帮助。
非常感谢您的帮助。

您的方法应该是这样的

char[][] array = new char[4][4];

public static void move(row, column){
    for (int i = 0, i < 4; i++) {
        for (int j = 0; j < 4; j++){
            if (array[i][j] != null) {
                // add rows and column accordingly
                array[i + row][j + column] = array[i][j];
                array[i][j] = null;
            }
        }
    }
}
char[][]数组=新字符[4][4];
公共静态无效移动(行、列){
对于(int i=0,i<4;i++){
对于(int j=0;j<4;j++){
if(数组[i][j]!=null){
//相应地添加行和列
数组[i+行][j+列]=数组[i][j];
数组[i][j]=null;
}
}
}
}
这是考虑到每行只有一个
x
,在您的情况下,有些行有两个。我会让你想出来的。

intmoverow=0;
    int moverow = 0;
    int moveCol = 2;

    for(int i = 0; i <=3; i++){
        for(int j = 0; j <=3; j++){
            int currentValue = board[i][j];
            //shifting value
            int shiftX = i + moverow;
            int shiftY = j + moveCol;
            // discarding the value if index overflows
            if(shiftX > 3 || shiftY > 3){

                // setting initial value on the original index.
                board[i][j] = 0;
                break;
            }else{
                board[shiftX][shiftY] = currentValue;
                // setting initial value on the original index.
                board[i][j] = 0;
            }
        }
    }
int-moveCol=2; 对于(int i=0;i 3){ //在原始索引上设置初始值。 板[i][j]=0; 打破 }否则{ 板[shiftX][shiftY]=当前值; //在原始索引上设置初始值。 板[i][j]=0; } } }
首先对标准1D阵列执行此操作。从那里很容易,只需在2D数组中的每个1D数组中循环。你确定
(0,2)
的预期结果会是这样吗?!创建一个新的二维数组,将第一个数组中的数据放入其中,偏移量对应于(行、列)参数。试着做点什么,如果你撞到了墙,过来告诉我们。运动是循环的吗?i、 e.如果有人传递了值(1,3),那么第二列元素应该出现在第一列,或者应该丢失。您至少可以“尝试”,然后在碰到砖墙时返回。发布一个问题,因为它对学习没有帮助。另一个用户问了同样的问题,他们称之为“作业”(大概是家庭作业)。如果他们要求更多的帮助,你可能想推迟代码。他删除了这个问题。我还没有找到我想要的正确答案。你有机会帮忙吗?你需要什么帮助?另外,把你的代码放在文章的底部。详细解释你遇到的问题。
    int moverow = 0;
    int moveCol = 2;

    for(int i = 0; i <=3; i++){
        for(int j = 0; j <=3; j++){
            int currentValue = board[i][j];
            //shifting value
            int shiftX = i + moverow;
            int shiftY = j + moveCol;
            // discarding the value if index overflows
            if(shiftX > 3 || shiftY > 3){

                // setting initial value on the original index.
                board[i][j] = 0;
                break;
            }else{
                board[shiftX][shiftY] = currentValue;
                // setting initial value on the original index.
                board[i][j] = 0;
            }
        }
    }