Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print 2D Arrays to look like a grid/matrix?

I'm trying to print Sudoku like grids. The desire output would be:

    int [][] easyGrid = {{2, 3, 5, 9, 7, 1, 8, 4, 6},
                                    {4, 7, 1, 2, 8, 6, 9, 3, 5},
                                    {8, 9, 6, 4, 3, 5, 2, 7, 1},
                                    {1, 6, 8, 7, 4, 9, 5, 2, 3},
                                    {7, 4, 9, 5, 2, 3, 1, 6, 8},
                                    {3, 5, 2, 1, 6, 8, 4, 9, 7},
                                    {6, 2, 4, 8, 1, 7, 3, 5, 9},
                                    {9, 1, 3, 6, 5, 2, 7, 8, 4},
                                    {5, 8, 7, 3, 9, 4, 6, 1, 2}}

;

But with System.out.println(Arrays.deepToString(easyGrid)); I get something like

[[2, 3, 5, 9, 7, 1, 8, 4, 6], [4, 7, 1, 2, 8, 6, 9, 3, 5], [8, 9, 6, 4, 3, 5, 2, 7, 1], [1, 6, 8, 7, 4, 9, 5, 2, 3], [7, 4, 9, 5, 2, 3, 1, 6, 8], [3, 5, 2, 1, 6, 8, 4, 9, 7], [6, 2, 4, 8, 1, 7, 3, 5, 9], [9, 1, 3, 6, 5, 2, 7, 8, 4], [5, 8, 7, 3, 9, 4, 6, 1, 2]]
like image 333
user45678 Avatar asked Feb 08 '23 00:02

user45678


2 Answers

One way might be to use a combination of StringJoiner (Java 8+) and String.format, for example

import java.util.StringJoiner;

public class Test {

    public static void main(String[] args) {
        int[][] easyGrid = {
            {2, 3, 5, 9, 7, 1, 8, 4, 6},
            {4, 7, 1, 2, 8, 6, 9, 3, 5},
            {8, 9, 6, 4, 3, 5, 2, 7, 1},
            {1, 6, 8, 7, 4, 9, 5, 2, 3},
            {7, 4, 9, 5, 2, 3, 1, 6, 8},
            {3, 5, 2, 1, 6, 8, 4, 9, 7},
            {6, 2, 4, 8, 1, 7, 3, 5, 9},
            {9, 1, 3, 6, 5, 2, 7, 8, 4},
            {5, 8, 7, 3, 9, 4, 6, 1, 2}};
        for (int[] row : easyGrid) {
            StringJoiner sj = new StringJoiner(" | ");
            for (int col : row) {
                sj.add(String.format("%02d", col));
            }
            System.out.println(sj.toString());
        }
    }

}

Which outputs...

02 | 03 | 05 | 09 | 07 | 01 | 08 | 04 | 06
04 | 07 | 01 | 02 | 08 | 06 | 09 | 03 | 05
08 | 09 | 06 | 04 | 03 | 05 | 02 | 07 | 01
01 | 06 | 08 | 07 | 04 | 09 | 05 | 02 | 03
07 | 04 | 09 | 05 | 02 | 03 | 01 | 06 | 08
03 | 05 | 02 | 01 | 06 | 08 | 04 | 09 | 07
06 | 02 | 04 | 08 | 01 | 07 | 03 | 05 | 09
09 | 01 | 03 | 06 | 05 | 02 | 07 | 08 | 04
05 | 08 | 07 | 03 | 09 | 04 | 06 | 01 | 02

You can generate some very complex results with these. Have a look at formatted strings for more details

For example...

import java.util.StringJoiner;

public class Test {

    public static void main(String[] args) {
        int[][] easyGrid = {
            {2, 3, 5, 9, 7, 1, 8, 4, 6},
            {4, 7, 1, 2, 8, 6, 9, 3, 5},
            {8, 9, 6, 4, 3, 5, 2, 7, 1},
            {1, 6, 8, 7, 4, 9, 5, 2, 3},
            {7, 4, 9, 5, 2, 3, 1, 6, 8},
            {3, 5, 2, 1, 6, 8, 4, 9, 7},
            {6, 2, 4, 8, 1, 7, 3, 5, 9},
            {9, 1, 3, 6, 5, 2, 7, 8, 4},
            {5, 8, 7, 3, 9, 4, 6, 1, 2}};
        String lineSplit = "";
        StringJoiner splitJoiner = new StringJoiner("+", "|", "|");
        for (int index = 0; index < easyGrid[0].length; index++) {
            splitJoiner.add(String.format("%4s", "").replace(" ", "-"));
        }
        lineSplit = splitJoiner.toString();
        for (int[] row : easyGrid) {
            StringJoiner sj = new StringJoiner(" | ", "| ", " |");
            for (int col : row) {
                sj.add(String.format("%02d", col));
            }
            System.out.println(lineSplit);
            System.out.println(sj.toString());
        }
        System.out.println(lineSplit);
    }

}

Which outputs

|----+----+----+----+----+----+----+----+----|
| 02 | 03 | 05 | 09 | 07 | 01 | 08 | 04 | 06 |
|----+----+----+----+----+----+----+----+----|
| 04 | 07 | 01 | 02 | 08 | 06 | 09 | 03 | 05 |
|----+----+----+----+----+----+----+----+----|
| 08 | 09 | 06 | 04 | 03 | 05 | 02 | 07 | 01 |
|----+----+----+----+----+----+----+----+----|
| 01 | 06 | 08 | 07 | 04 | 09 | 05 | 02 | 03 |
|----+----+----+----+----+----+----+----+----|
| 07 | 04 | 09 | 05 | 02 | 03 | 01 | 06 | 08 |
|----+----+----+----+----+----+----+----+----|
| 03 | 05 | 02 | 01 | 06 | 08 | 04 | 09 | 07 |
|----+----+----+----+----+----+----+----+----|
| 06 | 02 | 04 | 08 | 01 | 07 | 03 | 05 | 09 |
|----+----+----+----+----+----+----+----+----|
| 09 | 01 | 03 | 06 | 05 | 02 | 07 | 08 | 04 |
|----+----+----+----+----+----+----+----+----|
| 05 | 08 | 07 | 03 | 09 | 04 | 06 | 01 | 02 |
|----+----+----+----+----+----+----+----+----|
like image 75
MadProgrammer Avatar answered Feb 10 '23 12:02

MadProgrammer


Because in http://www.tutorialspoint.com/java/util/arrays_deeptostring.htm says:

The java.util.Arrays.deepToString(Object[]) method returns a string representation of the "deep contents" of the specified array. If the array contains other arrays as elements, the string representation contains their contents and so on. This method is designed for converting multidimensional arrays to strings. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

The solution is you have to cut the array in the 2d array, then you continuous cut every number in this array. After that, you print all the number in each array you cut. This is exactly what you need:

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        int [][] easyGrid = {{2, 3, 5, 9, 7, 1, 8, 4, 6},
                {4, 7, 1, 2, 8, 6, 9, 3, 5},
                {8, 9, 6, 4, 3, 5, 2, 7, 1},
                {1, 6, 8, 7, 4, 9, 5, 2, 3},
                {7, 4, 9, 5, 2, 3, 1, 6, 8},
                {3, 5, 2, 1, 6, 8, 4, 9, 7},
                {6, 2, 4, 8, 1, 7, 3, 5, 9},
                {9, 1, 3, 6, 5, 2, 7, 8, 4},
                {5, 8, 7, 3, 9, 4, 6, 1, 2}};
        for(int[] arr : easyGrid) {
            for(int number: arr) {
                System.out.print(number + " ");
            }
            System.out.println();
        }
    }
}

The result will be:

enter image description here

like image 29
Khuong Avatar answered Feb 10 '23 13:02

Khuong