java:将char[][]数组中的元素移动一个空格,可以是北、南、东、西等等。

java:将char[][]数组中的元素移动一个空格,可以是北、南、东、西等等。,java,arrays,methods,char,Java,Arrays,Methods,Char,我有一个任务,需要将10x10字符数组的元素移动一个位置,可以是北、南、东、西,也可以是东北、西北等等 我编写了一个方法,用w/B填充bear数组,用F填充fish数组,或用空格填充null数组 然后我写了一个方法来移动元素,但是我一直得到错误 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 我不知道出了什么问题,因为我在编译之前没有收到任何错误 这是我的密码: package project1final; public class

我有一个任务,需要将10x10字符数组的元素移动一个位置,可以是北、南、东、西,也可以是东北、西北等等

我编写了一个方法,用w/B填充bear数组,用F填充fish数组,或用空格填充null数组

然后我写了一个方法来移动元素,但是我一直得到错误 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:-1


我不知道出了什么问题,因为我在编译之前没有收到任何错误

这是我的密码:

package project1final;
public class Project1Final {
    public static void main(String[] args) {
        char [][] river = new char[10][10]; //2D array to simulate a river 
        fill_river(river); //fill array river 
        printArray(river); //print array river  
        move_elements(river); 
        System.out.println(); 
        printArray(river); 
    }

    //method to fill river matrix
    public static char[][] fill_river(char[][] river) {
        char bear = 'B'; //bear 
        char fish = 'F'; //fish 
        for (int i = 0; i < river.length; i++) {
            for (int j = 0; j < river[0].length; j++) {
                int random = (int)(Math.random() * 3 + 1); //generate random number 
                if (random == 1) {
                    river[i][j] = bear; 
                } //random == 1 = bear 
                else if (random == 2) {
                    river[i][j] = fish; 
                } //else if random == 2 = fish 
                else {
                    river[i][j] = ' '; 
                } //else = null
            } //for j loop
        } //for i loop    
        return river; //return filled matrix river 
    }

    //print array method 
    public static void printArray(char river[][]) {
        int count = 0; //count variable 
        for (int i = 0; i < river.length; i++) {
            for (int j = 0; j < river[0].length; j++) {
                System.out.print(river[i][j] + " "); //print 
                count++; //increment the count 
                if (count == river.length) {
                    System.out.println(); //print new line 
                    count = 0; //count starts over
                } //print a new line 
            } //for j loop
       } //for i loop
    }

    //method to move river elements 
    public static char[][] move_elements(char[][] river) {
        int move = (int)(Math.random() * 4 + 1); //random number 1-4
        for (int i = 0; i < river.length; i++) {
            for (int j = 0; j < river[0].length; j++) {  
                while (river[i][j] > river[i-1][j-1] && river[i][j] < river[i+1][j+1]) {
                    if (move == 1) {
                        river[i][j] = river[i+1][j]; //moves south 
                    }
                    else if (move == 2) {
                        river[i][j] = river[i-1][j]; //moves north  
                    }
                    else if (move == 3) {
                        river[i][j] = river[i][j+1]; //moves east  
                    }
                    else if (move == 4) {
                        river[i][j] = river[i][j-1]; //moves west  
                    }
                }
            } //for loop j
        } //for loop i
        return river; 
    }
}
packageproject1final;
公共类项目1最终{
公共静态void main(字符串[]args){
char[][]河=新的char[10][10];//模拟河的2D数组
填充_河(河);//填充阵列河
printary(river);//print array river
移动_元素(河流);
System.out.println();
打印阵列(河流);
}
//河流矩阵的填充方法
公共静态字符[][]填充_河(字符[][]河){
char bear='B';//bear
char fish='F';//fish
对于(int i=0;i河流[i-1][j-1]和河流[i][j]<河流[i+1][j+1]){
如果(移动==1){
河流[i][j]=河流[i+1][j];//向南移动
}
else if(move==2){
河流[i][j]=河流[i-1][j];//向北移动
}
else if(move==3){
河流[i][j]=河流[i][j+1];//向东移动
}
else if(move==4){
河流[i][j]=河流[i][j-1];//向西移动
}
}
}//对于循环j
}//对于循环i
回流河;
}
}

同样,我的问题是:如何使用方法将数组的元素(B、F和“”)移动一个空间

您肯定正在访问越界内存:

while(河流[i][j]>河流[i-1][j-1]和河流[i][j]<河流[i+1][j+1]

在代码周围的许多其他
if
语句中。

您可以看到
river[i+1][j+1]
在到达
river
数组末尾时,将如何尝试访问越界内存。

在代码中,如果元素位于river的“边界”,也会移动元素

您需要检查是否可以这样做,例如:

if (move == 1 && i + 1 < river.length) {
    river[i][j] = river[i+1][j]; //moves south ONLY if you can go south 
}
if(move==1&&i+1
如果你试图向南走,但不可能,会发生什么? 有许多可能的替代方案:

  • 你停在那里(不可能的话不要动)
  • 你颠倒了运动方向(因此,相反,向南,向北)
  • 从数组的底部传递到顶部(因此从底部传递到顶部)

根据您的选择,您可以有不同的代码。前面显示的示例是第一种可能性。

我怀疑这是家庭作业,发布整个项目并期望Stackoverflow解决这个问题不是一个体面的举动

我希望我们使用
纸+笔
调试算法:

  • 拿一张纸
  • 画出你的阵型。(它是10x10,不难画。)
  • 为数组创建打印方法,并根据打印结果绘制数组中的项目
  • 尝试在纸上使用
    move_元素
    这是一个比仅仅告诉您代码有缺陷的原因更有价值的答案。欢迎来到调试世界:)

    无论如何,问题是您没有处理代码的
    最坏情况

    假设你在最北/最南/最东/最西的位置有一只
    鱼/熊。如果骰子掷2,那么您的实现将把它移到
    河[-1][…]
    。这是数组的有效索引吗???这就是为什么您会得到
    索引自动边界异常
    。添加更多
    流控制语句(if)
    以处理这些情况


    如果您使用上述
    纸+笔
    方法,您将能够更轻松地解决此问题。

    您正在尝试访问阵列中不存在的位置。 这是一个简单的例子,简单而类似于你想要的,你只需要从这里得到想法和广告
    public class MoveArray{
    
        static void printArray(String [][] myArray) {
            for (String[] strings : myArray) {
                for (String string : strings) {
                    System.out.print(string + " ");
                }
                System.out.println();
            }
        }
    
        static String[][] moveRight(String [][] myArray) {
            for (int i = 0; i < myArray.length; i++) {
                for (int j = myArray.length-1; j > 0 ; j--) {
                    myArray[i][j] = myArray[i][j-1];
                    if (j == 1) myArray[i][j-1] = "0";
                }
            }
            return myArray;
        }
    
        static String[][] moveLeft(String [][] myArray) {
            for (int i = 0; i < myArray.length; i++) {
                for (int j = 0; j < myArray.length-1; j++) {
                    myArray[i][j] = myArray[i][j+1];
                    if (j == myArray.length-2) myArray[i][j+1] = "0";
                }
            }
            return myArray;
        }
    
        public static void main(String[] args) {
            String [][] s = {{"1","1","1"},{"1","1","1"},{"1","1","1"}};
            printArray(s);
    
            System.out.println("\nMove right");
            printArray(moveRight(s));
    
            System.out.println("\nMove left");
            printArray(moveLeft(s));
        }
    }