java:将char [] []数组中的元素向北,向南,向东,向西等方向移动一个空格. [英] java: Move elements in a char[][] array by one space either north, south, east, west, etc.

查看:54
本文介绍了java:将char [] []数组中的元素向北,向南,向东,向西等方向移动一个空格.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了一项作业,其中我需要将10x10字符数组的元素向北,向南,向东,向西或向北,向北,向西移动一个位置.

I had an assignment wherein I need to move elements of a 10x10 char array by one position either north, south, east, west, or NE, NW, etc.

我写了一种方法来填充数组w/B表示熊,F表示鱼,或者为null填充空格.

I wrote a method to fill the array w/ B for bear and F for fish, or a blank space for null.

然后,我编写了一种移动元素的方法,但是我不断收到错误消息线程主"中的异常java.lang.ArrayIndexOutOfBoundsException:-1"

I then wrote a method to move the elements, but i keep getting the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1"

我不知道这是怎么回事,因为我在编译之前没有收到任何错误.

I have no clue what's wrong since im not getting any errors before compiling.

这是我的代码:

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

同样,我的问题是:如何使用一种方法将数组的元素(B,F和")移动一个空格.

Again, my question is: how do I move the elements of my array (B, F and " ") by one space using a method.

推荐答案

您正尝试访问数组中不存在的位置.这是一个简单的示例,类似于您想要的简单内容,您只需要从这里获取点子并修改您的代码即可:

You are trying to access a position that does not exist in your array. Here is a simple example of something simple and similar to what you want, you just need to get the idea from here and adapt your code:

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

这篇关于java:将char [] []数组中的元素向北,向南,向东,向西等方向移动一个空格.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆