xVegaz
xVegaz

Reputation: 23

Create a 2d array in a square shape

I have to create a 2d array that looks like on the picture:

2d Arrays

I'm trying like this but I'm unsure:

public static void main(String[] args) {
    String[][] zeile1 = {"- - - - - - -"};
    String[][] zeile2 = {"|           |"};
    String[][] zeile3 = {"|           |"};
    String[][] zeile4;
}

Upvotes: 2

Views: 1310

Answers (3)

lasso
lasso

Reputation: 1

If I understood the problem correctly, I would propose this solution:

  • conceptually, you need two functions:
  • one to initialise floor an ceiling ---------------
static void initialiseCeilingAndFloor(char plane[][], int n) {
    for (int i = 0; i < plane.length; i++) {
        plane[0][i] = '-'; // ceiling is initialised
        plane[plane.length - 1][i] = '-'; //floor is initialised
    }
}

The other one to draw one line of each wall |. . . . . . . . .|. Here I actually broke this method in two, for clarity: InitialiseInternalRow does what it says, and InitialiseBody uses it to initialise the whole body of the plane

static void initialiseBody(char plane[][]) {
    // we already initialised floor and ceiling, so here we'll
    for (int i = 1; i < plane.length - 1; i++) {
        // reduce the cycle at the internal lines
        initialiseInternalRow(plane, i);
    }
}
static void initialiseInternalRow(char plane[][], int i) {
    for (int j = 0; j < plane.length; j++) {
        // if we are at the extremes, we draw a wall: '|'
        if (j == 0 || j == plane.length - 1)
            plane[i][j] = '|';
        else // otherwise we leave space
            plane[i][j] = ' ';
    }
}

So that you can easily initialise your array in your main:

char plane[][] = new char[n][n];
initialiseCeilingAndFloor(plane);
initialiseBody(plane);

You could even think to wrap the two methods together to initialise the thing in one hit, like this:

static void initialisePlane(char plane[][]) {
    initialiseCeilingAndFloor(plane);
    initialiseBody(plane);
}

So that you could easily call from your main:

char plane[][] = new char[n][n];
initialisePlane(plane);

No worries if your terminal displays the output more as a rectangle. this depends on the formatting used by the particular application you are using to display the text. just replace the space with dots, and count them. Serious testings here.

Upvotes: 0

user14940971
user14940971

Reputation:

You can use streams to create such an array:

int m = 5;
String[][] arr = IntStream.range(0, m)
        .mapToObj(i -> IntStream.range(0, m)
                .mapToObj(j -> {
                    String val = " ";
                    if (i == 0 || i == m - 1) {
                        val += "-";
                    } else if (j == 0 || j == m - 1) {
                        val += "|";
                    } else {
                        val += " ";
                    }
                    return val;
                }).toArray(String[]::new))
        .toArray(String[][]::new);
// output
Arrays.stream(arr).map(row -> String.join("", row)).forEach(System.out::println);

Output:

 - - - - -
 |       |
 |       |
 |       |
 - - - - -

See also:
How do I return such an multi-dimensional array?
Drawing a rectangle with asterisks using methods and loops

Upvotes: 0

Aalexander
Aalexander

Reputation: 5004

Pattern

The pattern here is

  • in the first and in the last row you have to print the dash "-".
  • in the first and the last column you have to print a "|".
  • For the other fields here we are just printing blank whitespaces " ";

Solution

  • So you declare a 2D Array

String[][] grid = new String[5][5]; // chose your dimension
  • Iniitialize your grid loop through both dimensions and do the check for the 3 cases mentioned in the section Pattern.
    Note i corresponds to your rows and j corresponds to your columns This means i=0 is your first row and grid.length-1 is your last row Same applies to the columns.

if (i == 0 || i == grid.length - 1) {
    grid[i][j] = "-";
} else if (j == 0 || j == grid[i].length - 1) {
    grid[i][j] = "|";
} else {
    grid[i][j] = " ";
}

The Code

public static void main(String[] args) {
    String[][] grid = new String[5][5];
    for (int i = 0; i < grid.length; i++) {
        for (int j = 0; j < grid[i].length; j++) {
            if (i == 0 || i == grid.length - 1) {
                grid[i][j] = "-";
            } else if (j == 0 || j == grid[i].length - 1) {
                grid[i][j] = "|";
            } else {
                grid[i][j] = " ";
            }
        }
    }
}

Output

-----
|   |
|   |
|   |
-----

Upvotes: 1

Related Questions