如何将二维数组打印成 grid/matrix 的样子?
How to print 2D Arrays to look like a grid/matrix?
我正在尝试像网格一样打印数独。期望输出为:
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}}
;
但是 System.out.println(Arrays.deepToString(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]]
因为在 http://www.tutorialspoint.com/java/util/arrays_deeptostring.htm 中说:
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).
解决方法是你必须切割二维数组中的数组,然后你连续切割这个数组中的每个数字。之后,打印您切割的每个数组中的所有数字。这正是您所需要的:
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();
}
}
}
结果将是:
一种方法可能是使用 StringJoiner
(Java 8+)和 String.format
的组合,例如
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());
}
}
}
哪个输出...
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
您可以使用这些生成一些非常复杂的结果。查看 formatted strings 了解更多详情
例如...
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);
}
}
输出
|----+----+----+----+----+----+----+----+----|
| 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 |
|----+----+----+----+----+----+----+----+----|
您使用 Arrays
class 中的一种打印方法的想法是正确的。但是,使用 Arrays.deepToString()
,您最终会得到 one-liner 输出,这不是您想要的。相反,您可以遍历 two-dimensional 数组的行并在每一行上使用 Arrays.toString()
:
for (int i=0; i < easyGrid[0].length; ++i) {
System.out.println(Arrays.toString(easyGrid[i]));
}
输出:
[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]
我正在尝试像网格一样打印数独。期望输出为:
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}}
;
但是 System.out.println(Arrays.deepToString(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]]
因为在 http://www.tutorialspoint.com/java/util/arrays_deeptostring.htm 中说:
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).
解决方法是你必须切割二维数组中的数组,然后你连续切割这个数组中的每个数字。之后,打印您切割的每个数组中的所有数字。这正是您所需要的:
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();
}
}
}
结果将是:
一种方法可能是使用 StringJoiner
(Java 8+)和 String.format
的组合,例如
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());
}
}
}
哪个输出...
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
您可以使用这些生成一些非常复杂的结果。查看 formatted strings 了解更多详情
例如...
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);
}
}
输出
|----+----+----+----+----+----+----+----+----|
| 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 |
|----+----+----+----+----+----+----+----+----|
您使用 Arrays
class 中的一种打印方法的想法是正确的。但是,使用 Arrays.deepToString()
,您最终会得到 one-liner 输出,这不是您想要的。相反,您可以遍历 two-dimensional 数组的行并在每一行上使用 Arrays.toString()
:
for (int i=0; i < easyGrid[0].length; ++i) {
System.out.println(Arrays.toString(easyGrid[i]));
}
输出:
[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]