简体   繁体   中英

How to change the first row and column in a 2D array

Hello I'm fairly new to java and am creating a game that involves a 2D array. I'm trying to have a line at the top and side of the grid to display 0-10 and AJ which will be coordinates of the grid. I've made a simple 2D array:

String[][] board = new String [10][10];

    for (int row = 0; row<board.length;row++) {
        for (int column = 0; column<board.length; column++) {
            board[row][column] = ".";
            System.out.print(board[row][column] + "  ");
        }
        System.out.println();
    }

I've done some trial and error but I can't seem to find a solution and there's not much on this I can find on google. Any advice will be appreciated

Try something more like this:

public static void main(String[] args)
{
    String[][] board = new String [10][10];
    for (int row = 0; row<board.length;row++) {
        for (int column = 0; column<board[0].length; column++) {
            board[row][column] = ".";
        }
    }

    System.out.println();
    displayBoard(board);

    // change something in the board
    board[2][3] = "*";

    System.out.println();
    displayBoard(board);
}

private static void displayBoard(String[][] board)
{   
    String rowLabels = "ABCDEFGHIJ";
    System.out.println("   0  1  2  3  4  5  6  7  8  9");
    for (int row = 0; row<board.length;row++) {
        System.out.print(rowLabels.substring(row, row+1) + "  ");
        for (int column = 0; column<board[row].length; column++) {
            System.out.print(board[row][column] + "  ");
        }
        System.out.println();
    }
}

Output:

   0  1  2  3  4  5  6  7  8  9
A  .  .  .  .  .  .  .  .  .  .  
B  .  .  .  .  .  .  .  .  .  .  
C  .  .  .  .  .  .  .  .  .  .  
D  .  .  .  .  .  .  .  .  .  .  
E  .  .  .  .  .  .  .  .  .  .  
F  .  .  .  .  .  .  .  .  .  .  
G  .  .  .  .  .  .  .  .  .  .  
H  .  .  .  .  .  .  .  .  .  .  
I  .  .  .  .  .  .  .  .  .  .  
J  .  .  .  .  .  .  .  .  .  .  

   0  1  2  3  4  5  6  7  8  9
A  .  .  .  .  .  .  .  .  .  .  
B  .  .  .  .  .  .  .  .  .  .  
C  .  .  .  *  .  .  .  .  .  .  
D  .  .  .  .  .  .  .  .  .  .  
E  .  .  .  .  .  .  .  .  .  .  
F  .  .  .  .  .  .  .  .  .  .  
G  .  .  .  .  .  .  .  .  .  .  
H  .  .  .  .  .  .  .  .  .  .  
I  .  .  .  .  .  .  .  .  .  .  
J  .  .  .  .  .  .  .  .  .  .  

Note that the headers are NOT being stored in the array itself, as then you'd have to do extra processing to determine the "state" of what is in the array...

Check if you are on the first row and assign number depending on column, and check if you are on the first column and assign symbol depending on row. Try this inside your loops:

if(row == 0) {
  board[row][column] = column;
} else if(column == 0) {
  board[row][column] = "" + ('A' + row);
} else {
  board[row][column] = ".";
}
System.out.print(board[row][column] + "  ");

EDIT There will be problem with the corner - should it be A or 0 ? You may want to start iterating from 1 instead of 0. Additionally, as someone pointed out in comment - for 0-10 you will need 11 elemtent. Additionally if you want a corner it might grow to 12!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM