JAVA Two-Dimensional Array

Asked 2 years ago, Updated 2 years ago, 30 views

static final int ROW = 4;
static final int COL = 3;

public static void main(String[] args) {
    String[][] parkingSpace = new String[ROW][COL];

    System.out.println(" \t1\t2\t3");
    String fill = "□";

    for(int i=0; i<=parkingSpace[ROW].length + 1; i++) {
        System.out.print(i);
        for(int j=0; j<=parkingSpace[COL].length + 1; j++) {
            if(parkingSpace[i][j] != null) {
                fill = "■";
                System.out.print('\t' + fill);
            } } else {
                System.out.print('\t' + fill);
            }
            }
        System.out.println();
        }

If all the rest are empty and there are values only for (2, 3), When outputting

    1     2     3
1   □    □    □
2   □    □    ■
3   □    □    □
4   □    □    □

I'd like it to come out as However, only the first line is printed and an error occurs after that. I want you to tell me how to print it out as you want.

java

2022-09-25 01:00

1 Answers

parkingSpace[ROW].length and parkingSpace[COL].Length seems to be causing the problem.
I think you can solve it by checking what the two values are in println before executing the for statement.


2022-09-25 01:00

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.