我遇到二维数组问题。我有这样的显示器:
1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 . . . etc
基本上我想要的是显示它显示为:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20
21 22 23 24 ... etc
这是我的代码:
int twoDm[][]= new int[7][5];
int i,j,k=1;
for(i=0;i<7;i++){
for(j=0;j<5;j++) {
twoDm[i][j]=k;
k++;}
}
for(i=0;i<7;i++){
for(j=0;j<5;j++) {
System.out.print(twoDm[i][j]+" ");
System.out.print("");}
}
答案 0 :(得分:13)
您需要在每行之后打印一个新行... System.out.print("\n")
,或使用println
等。目前您只需打印任何内容 - System.out.print("")
,替换{{ 1}} print
或println
""
。
答案 1 :(得分:12)
如果你不介意逗号和括号,你可以简单地使用:
System.out.println(Arrays.deepToString(twoDm).replace("], ", "]\n")));
答案 2 :(得分:11)
您可以编写一个方法来打印这样的二维数组:
//Displays a 2d array in the console, one line per row.
static void printMatrix(int[][] grid) {
for(int r=0; r<grid.length; r++) {
for(int c=0; c<grid[r].length; c++)
System.out.print(grid[r][c] + " ");
System.out.println();
}
}
答案 3 :(得分:10)
public class FormattedTablePrint {
public static void printRow(int[] row) {
for (int i : row) {
System.out.print(i);
System.out.print("\t");
}
System.out.println();
}
public static void main(String[] args) {
int twoDm[][]= new int[7][5];
int i,j,k=1;
for(i=0;i<7;i++) {
for(j=0;j<5;j++) {
twoDm[i][j]=k;
k++;
}
}
for(int[] row : twoDm) {
printRow(row);
}
}
}
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
当然,您可以更换7&amp;如其他答案所述,每行7个。
答案 4 :(得分:3)
来自 @djechlin 的部分答案,您应该更改行和列。由于您被视为7行和5列,但实际上您需要的是7列和5行。
这样做: -
int twoDm[][]= new int[5][7];
for(i=0;i<5;i++){
for(j=0;j<7;j++) {
System.out.print(twoDm[i][j]+" ");
}
System.out.println("");
}
答案 5 :(得分:3)
除了代码之外,我将发布一个更详细的解决方案,因为最初的错误以及随后在注释中演示的错误是这种字符串连接问题中常见的错误。
从最初的问题开始,正如@djechlin充分解释的那样,我们发现在表格的每一行完成后需要打印一个新行。所以,我们需要这样的陈述:
System.out.println();
但是,在第一个打印语句之后立即打印会产生错误的结果。是什么给了什么?
1
2
...
n
这是范围问题。请注意,有两个循环是有原因的 - 一个循环处理行,而另一个循环处理列。对于给定的“i”,你的内部循环“j”循环遍历每个数组元素“j”。因此,在j循环结束时,您应该有一行。您可以将此“j”循环的每个迭代视为构建表的“列”。由于内循环构建了我们的列,我们不想在那里打印我们的行 - 它会为每个元素创建一个新行!
一旦你离开了j循环,你需要在继续下一个“i”迭代之前终止该行。这是处理新行的正确位置,因为它是表格行的“范围”,而不是表格的列。
for(i=0;i<7;i++){
for(j=0;j<5;j++) {
System.out.print(twoDm[i][j]+" ");
}
System.out.println();
}
即使您通过更改“i”和“j”循环的结束值来更改表格的尺寸,您也可以看到这一新行将成立。
答案 6 :(得分:3)
更多高效且简单的方法以格式化方式打印2D数组:
试试这个:
public static void print(int[][] puzzle) {
for (int[] row : puzzle) {
for (int elem : row) {
System.out.printf("%4d", elem);
}
System.out.println();
}
System.out.println();
}
示例输出:
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
答案 7 :(得分:2)
仅仅为了记录,Java 8提供了更好的选择。
int[][] table = new int[][]{{2,4,5},{6,34,7},{23,57,2}};
System.out.println(Stream.of(table)
.map(rowParts -> Stream.of(rowParts
.map(element -> ((Integer)element).toString())
.collect(Collectors.joining("\t")))
.collect(Collectors.joining("\n")));
答案 8 :(得分:0)
您可以创建一个将矩阵打印为表格的方法:
注意:对于具有多位数字的数字的矩阵,这不起作用 非平方矩阵。
public static void printMatrix(int size,int row,int[][] matrix){
for(int i = 0;i < 7 * size ;i++){
System.out.print("-");
}
System.out.println("-");
for(int i = 1;i <= matrix[row].length;i++){
System.out.printf("| %4d ",matrix[row][i - 1]);
}
System.out.println("|");
if(row == size - 1){
// when we reach the last row,
// print bottom line "---------"
for(int i = 0;i < 7 * size ;i++){
System.out.print("-");
}
System.out.println("-");
}
}
public static void main(String[] args){
int[][] matrix = {
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
{13,14,15,16}
};
// print the elements of each row:
int rowsLength = matrix.length;
for(int k = 0; k < rowsLength; k++){
printMatrix(rowsLength,k,matrix);
}
}
输出:
---------------------
| 1 | 2 | 3 | 4 |
---------------------
| 5 | 6 | 7 | 8 |
---------------------
| 9 | 10 | 11 | 12 |
---------------------
| 13 | 14 | 15 | 16 |
---------------------
我在练习循环和数组时创建了这个方法,我宁愿使用:
System.out.println(Arrays.deepToString(matrix).replace("], ", "]\n")));
答案 9 :(得分:0)
伊利亚,
很抱歉。
你的代码是有效的。但是它的Array行和列有一些问题
这里我正确地纠正了你的代码,你可以尝试这个..
public static void printMatrix(int size, int row, int[][] matrix) {
for (int i = 0; i < 7 * matrix[row].length; i++) {
System.out.print("-");
}
System.out.println("-");
for (int i = 1; i <= matrix[row].length; i++) {
System.out.printf("| %4d ", matrix[row][i - 1]);
}
System.out.println("|");
if (row == size - 1) {
// when we reach the last row,
// print bottom line "---------"
for (int i = 0; i < 7 * matrix[row].length; i++) {
System.out.print("-");
}
System.out.println("-");
}
}
public static void length(int[][] matrix) {
int rowsLength = matrix.length;
for (int k = 0; k < rowsLength; k++) {
printMatrix(rowsLength, k, matrix);
}
}
public static void main(String[] args) {
int[][] matrix = { { 1, 2, 5 }, { 3, 4, 6 }, { 7, 8, 9 }
};
length(matrix);
}
并且看起来像
----------------------
| 1 | 2 | 5 |
----------------------
| 3 | 4 | 6 |
----------------------
| 7 | 8 | 9 |
----------------------
答案 10 :(得分:0)
声明了一个与您相似的7 x 5数组,并带有一些虚拟数据。下面应该做。
int[][] array = {{21, 12, 32, 14, 52}, {43, 43, 55, 66, 72}, {57, 64, 52, 57, 88},{52, 33, 54, 37, 82},{55, 62, 35, 17, 28},{55, 66, 58, 72, 28}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println(); // the trick is here, print a new line after iterating first row.
}
答案 11 :(得分:-1)
public static void main(String[] args) {
int[][] matrix = {
{ 1, 2, 5 },
{ 3, 4, 6 },
{ 7, 8, 9 }
};
System.out.println(" ** Matrix ** ");
for (int rows = 0; rows < 3; rows++) {
System.out.println("\n");
for (int columns = 0; columns < matrix[rows].length; columns++) {
System.out.print(matrix[rows][columns] + "\t");
}
}
}
这样可行,在行的for循环中添加一个新行。当第一行打印完成后,代码将跳转到新行。
答案 12 :(得分:-1)
For traversing through a 2D array, I think the following for loop can be used.
for(int a[]: twoDm)
{
System.out.println(Arrays.toString(a));
}
if you don't want the commas you can string replace
if you want this to be performant you should loop through a[] and then print it.
答案 13 :(得分:-2)
你满意的一切我很满意我很惊讶它需要一点智商 只需通过arr [0]得到长度。长度和问题解决了
for (int i = 0; i < test.length; i++) {
for (int j = 0; j < test[0].length; j++) {
System.out.print(test[i][j]);
}
System.out.println();
}