简体   繁体   English

如何在表格中同时写入行索引和列索引(Java)-嵌套循环

[英]How to write BOTH row and column indexes in a table (java) - nested for loops

I want to output table that shows row and column indexes [i][j] for every cell , 我想输出显示每个单元格的行和列索引[i] [j]的表,

I can only print i-index but don't know how to print "column" index 我只能打印i-index,但不知道如何打印“ column”索引

It should look like this: 它看起来应该像这样: 表 for every column - there is indexes shown above - first horizontal line - 0 1 2 3 4 5 6 7 8 9 10 每列-上面显示了索引-第一条水平线-0 1 2 3 4 5 6 7 8 9 10

The code i have : 我有的代码:

public static void print_table( int table[][] ){
    System.out.println( "PRINTING TABLE ") ;

    for(int i = 0; i <= NCHANGES; i++) {
        System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW
        for(int j = 0; j <= MAX_AMOUNT; j++) {
            System.out.print(table[i][j] + "\t") ;
            // BUT HOW TO PRINT J(COLUMN) INDEX for every column?
        }
        System.out.println() ;
    }
}

I'm sure this will be useful for all who need to print out the result of their 2-nested loops 我相信这对于所有需要打印2个嵌套循环的结果的人都是有用的

Before doing the nested loops just make a for loop and print 在进行嵌套循环之前,只需进行for循环并打印即可
- the I/J cell -I / J单元
- the column header cells (0, 1, 2, etc.) -列标题单元格(0、1、2等)
- new line - 新队

Then do what you're already doing. 然后做你已经在做的。

Something like this perhaps? 大概是这样吗?

    public static void main(String[] args) {
    int[][] twoD = new int[][] { { 123, 456, 789 }, { 123, 456, 789 },
            { 123, 456, 789 } };

    StringBuilder sb = new StringBuilder();

    for (int I = 0; I < twoD.length; I++) {
        for (int X = 0; X < twoD[I].length; X++) {
            sb.append("Row " + I);
            sb.append(" Column " + X);
            sb.append(" Value " + twoD[I][X]);
            sb.append("\n");
            System.out.print(sb.toString());
            sb.setLength(0);
        }
    }
}

Console output: 控制台输出:

Row 0 Column 0 Value 123
Row 0 Column 1 Value 456
Row 0 Column 2 Value 789
Row 1 Column 0 Value 123
Row 1 Column 1 Value 456
Row 1 Column 2 Value 789
Row 2 Column 0 Value 123
Row 2 Column 1 Value 456
Row 2 Column 2 Value 789

Maybe this? 也许这个吗?

public static void print_table( int table[][] ){
 System.out.println( "PRINTING TABLE ") ;

 //Extra loop here
 System.out.print("I/J ");
 for (int j = 0; j <= MAX_AMOUNT; j++){
     System.out.print(j + " ");  
 }

 for(int i = 0 ; i <= NCHANGES ;i++){

   System.out.print("[" + i + "]") ;// this prints i-index for EVERY ROW

         for(int j = 0 ; j <= MAX_AMOUNT  ; j++){

      System.out.print(table[i][j] + "\t") ;// BUT HOW TO PRINT J(COLUMN) INDEX for every column?

                 }
                 System.out.println() ;
         }

 }
public static void print_table( int table[][] ){
 System.out.println( "\n\nPRINTING TABLE ") ;

 System.out.println("\t\tm") ;
  System.out.print("\n        ") ;
 for(int column = 0 ; column <= MAX_AMOUNT; column++){
           System.out.print(  column  + " \t"  ) ;
 }
 System.out.print("\n----------------------------------------------------------------------------------------------------------------------------------") ;
 System.out.println("----------------------------------------------------------") ;
 for(int i = 0 ; i <= NCHANGES ;i++){
   System.out.print("[" + i + "]:  ") ;
         for(int j = 0 ; j <= MAX_AMOUNT  ; j++){

                    System.out.print(table[i][j] + "\t") ;

                 }
                 System.out.println() ;
         }

 }

EXACTLY AS I WANTED IT! 就像我要的那样! 表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM