如何打印二维数组如表 [英] How to print Two-Dimensional Array like table

查看:162
本文介绍了如何打印二维数组如表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用二维阵列的一个问题。我有这样的显示:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16。 。 。等等

基本上什么我要的是要显示其显示为:

  1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20
21 22 23 24 ...等

下面是我的code:

  INT twoDm [] [] =新INT [7] [5];
    INT I,J,K = 1;        对于(I = 0; I&下; 7;我++){
            为(J = 0; J&小于5; J ++){
             twoDm [I] [J] = K;
                ķ++;}
        }        对于(I = 0; I&下; 7;我++){
            为(J = 0; J&小于5; J ++){
                System.out.print(twoDm [I] [J] +);
                System.out.print();}
        }


解决方案

 公共类FormattedTablePrint {    公共静态无效printRow(INT []行){
        对于(INT I:行){
            System.out.print(ⅰ);
            System.out.print(\\ t的);
        }
        的System.out.println();
    }    公共静态无效的主要(字串[] args){
        INT twoDm [] [] =新INT [7] [5];
        INT I,J,K = 1;        对于(I = 0; I&下; 7;我++){
            为(J = 0; J&小于5; J ++){
                twoDm [I] [J] = K;
                ķ++;
            }
        }        的for(int []排:twoDm){
            printRow(行);
        }
    }
}

输出

  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安培; 5在其他的答案中提到,要获得7%的行。

I'm having a problem with two dimensional array. I'm having a display like this:

1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 . . . etc

What basically I want is to display to display it as:

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

Here is my code:

    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("");}
        }

解决方案

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);
        }
    }
}

Output

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  

Of course, you might swap the 7 & 5 as mentioned in other answers, to get 7 per row.

这篇关于如何打印二维数组如表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆