简体   繁体   English

将2D阵列打印为网格? (Java)

[英]Print A 2D Array As A Grid? (Java)

I've run into a bit of a conundrum in a personal Java project I've been working on. 在我一直从事的个人Java项目中,我遇到了一个难题。 I want to print a two-dimensional array of Strings in the form of a table. 我想以表格的形式打印一个二维的字符串数组。 Not the Strings by themselves, but with row and column labels as well (think Microsoft Excel). 不是字符串本身,而是行和列标签(请考虑使用Microsoft Excel)。 This is what I envision the finished product to be, with asterisks representing where the Strings should go. 这就是我所预想的最终产品,带有星号的代表字符串应该放哪里。

    |    A    |    B    |    C    |    D    |    E    |    F    |    G    |
----+---------+---------+---------+---------+---------+---------+---------+
  1 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  2 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  3 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  4 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  5 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  6 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  7 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  8 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
  9 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+
 10 |    *    |    *    |    *    |    *    |    *    |    *    |    *    |
----+---------+---------+---------+---------+---------+---------+---------+

I know that this will use a nested forward loop, and that the logical process would be to put the String values in the inner loop, like "example[i][j]" type format. 我知道这将使用嵌套的前向循环,并且逻辑过程是将String值放入内部循环,例如“ example [i] [j]”类型格式。 I'm just so confused as to how I go about getting the design around the cells in the right format, limiting each String to 10 characters like how Excel limits their cell size when shrunken down. 我对如何以正确的格式围绕单元格进行设计感到困惑,将每个String限制为10个字符,就像Excel缩小时如何限制其单元格大小一样。 Do I use substring for that? 我是否为此使用子字符串? Do I use printf to get the 10th row correctly spaced? 我是否使用printf来使第10行正确间隔?

Any pointers are greatly appreciated, I've never been stumped quite like this before. 任何指针都将不胜感激,我从未像现在这样陷入困境。

The first line should be easy enough, assuming you don't exceed 26 columns, ie column name is just A to Z . 第一行应该很容易,假设您不超过26列,即列名只是AZ

The even lines are all a lead-in of ----+ , followed by columnCount repeats of ---------+ . 偶数行都是----+的导入,其后是---------+ columnCount重复。

The odd lines (except first), are a lead-in of 999 | 奇数行(第一行除外)的引入为999 | , where 999 is a row number, right-justified, with leading spaces. ,其中999是右对齐的行号,前导空格。 That can be done with printf() or String.format() with a format string of 可以使用printf()String.format()的格式字符串为
"%3d |" .

Following the lead-in are columnCount repeats of a string value, trimmed and center-aligned to 9 characters, followed by a | 在导入之后,是字符串计数的columnCount重复项,将其修剪并居中对齐为9个字符,后跟| .

To center-align to 9 characters, do the following: 要居中对齐9个字符,请执行以下操作:

  • If length > 9, trim to 9 (yes, using substring() ). 如果长度> 9,则修剪为9(是,使用substring() )。
  • Otherwise, calculate spaces needed, ie spacesTotal = 9 - trimmedLength . 否则,计算所需的空间,即spacesTotal = 9 - trimmedLength
    Calculate spaces on left: spaceBefore = spacesTotal / 2 . 计算左侧的空间: spaceBefore = spacesTotal / 2
    Calculate spaces on right: spaceAfter = spacesTotal - spaceBefore . 计算右边的空间: spaceAfter = spacesTotal - spaceBefore
    By doing it that way, an odd number of spaces such as 5, becomes 2 before and 3 after. 通过这种方式,奇数个空格(例如5)在2之前和3之后。
    Now print spaceBefore spaces, the (trimmed) text value, then spaceAfter spaces, and a | 现在打印spaceBefore ,(修剪后的)文本值, spaceAfter| .
public static void printStringGrid(String[][] array){
    System.out.print("    |");
    for (int i = 0; i < array[0].length; i++){
        System.out.print("    ");
        System.out.print((char)('A' + i));
        System.out.print("    |");
    }
    System.out.println();
    for (int i = 0; i < array.length; i++){
        System.out.print("----+");
        for (int j = 0; j < array[0].length; j++){
            System.out.print("---------+");
        }
        System.out.println();
        System.out.print("  " + (i + 1) + " |");
        for (int j = 0; j < array[0].length; j++){
            if (array[i][j].length() < 10){
                int spaces = (9 - array[i][j].length()) / 2;
                for (int k = 0; k < spaces; k++){
                    System.out.print(" ");
                }
                System.out.print(array[i][j]);
                for (int k = 0; k < (9 - array[i][j].length()) - spaces; k++){
                    System.out.print(" ");
                }
            }
            else{
                System.out.print(array[i][j].substring(0, 9));
            }
            System.out.print("|");
        }
        System.out.println();
    }
}

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

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