简体   繁体   中英

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. 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). 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. 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. Do I use substring for that? Do I use printf to get the 10th row correctly spaced?

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 .

The even lines are all a lead-in of ----+ , followed by columnCount repeats of ---------+ .

The odd lines (except first), are a lead-in of 999 | , where 999 is a row number, right-justified, with leading spaces. That can be done with printf() or String.format() with a format string of
"%3d |" .

Following the lead-in are columnCount repeats of a string value, trimmed and center-aligned to 9 characters, followed by a | .

To center-align to 9 characters, do the following:

  • If length > 9, trim to 9 (yes, using substring() ).
  • Otherwise, calculate spaces needed, ie spacesTotal = 9 - trimmedLength .
    Calculate spaces on left: spaceBefore = spacesTotal / 2 .
    Calculate spaces on right: spaceAfter = spacesTotal - spaceBefore .
    By doing it that way, an odd number of spaces such as 5, becomes 2 before and 3 after.
    Now print spaceBefore spaces, the (trimmed) text value, then spaceAfter spaces, and a | .
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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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