Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

java - Formatting 2d array of numbers

Problem:
I have 2d array which is filled with numbers. I have to output it in a way that it shows: "*" between neighbours with different values and with " " if values are the same. Example:

*********
*1 1*3*4*
***** * *
*2 2*3*4*
*********

I have tried many things like creating another array with [Nx2][Mx2] size or System.out.format, but in the end it's never formatted the way I like. Any suggestions how can I solve this?

private static void changeColumn(int[][] secondLayerArr, int n, int m) {
    String[][] finalLayerArr = new String[n * 2 - 1][m];
    int finalLayerRow = -2;
    //second layer output
    for (int i = 0; i < n; i++) {
        finalLayerRow += 2;

        for (int j = 0; j < m; j++) {
            if (j < m - 1) {
                if (secondLayerArr[i][j] != secondLayerArr[i][j + 1]) {
                    finalLayerArr[finalLayerRow][j] = (secondLayerArr[i][j]) + "*";
                    //  System.out.print(secondLayerArr[i][j] + "*");
                } else {
                    finalLayerArr[finalLayerRow][j] = (secondLayerArr[i][j]) + " ";
                    //  System.out.print(secondLayerArr[i][j]);
                }
            } else {
                finalLayerArr[finalLayerRow][j] = (secondLayerArr[i][j]) + "*";
                //System.out.print(secondLayerArr[i][j]+"*");
            }
        }
    }
    printColumn(finalLayerArr);
}
public static void changeRow(String[][] finalLayerArr) {
    for (int i = 0; i < finalLayerArr[0].length; i++) {
        System.out.print("***");
    }
    System.out.println();

    for (int i = 0; i < finalLayerArr.length; i++) {
        System.out.print("*");
        for (int j = 0; j < finalLayerArr[0].length; j++) {
            if (finalLayerArr[i][j] == null) {
                if (finalLayerArr[i - 1][j].equals(finalLayerArr[i + 1][j])) {
                    finalLayerArr[i][j] = " ";
                } else {
                    finalLayerArr[i][j] = "*";
                }
            }
            System.out.printf("%2s", finalLayerArr[i][j], "");
        }
        System.out.println();
    }
}

It shows something like the result I want but its not formatted like in table.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could loop through every line that isn't the last, then every letter that isn't the last in that array, and checks if it is equal to the one to the right and the one to the bottom. If it is, print the appropriate thing.

Something along these lines:

public class FormattingArray {
    public static void printFormattedInts(int[][] unformattedInts) {

        // getting maximum digits per number

        int maxDigits = 0;

        for (int[] intArray : unformattedInts) {
            for (int num : intArray) {
                if (lengthOfInt(num) > maxDigits) {
                    maxDigits = lengthOfInt(num);
                }
            }
        }


        // printing first line (purely aesthetic)

        System.out.print("*".repeat(unformattedInts[0].length * maxDigits + unformattedInts[0].length + 1));

        System.out.println();

        // printing each row


        for (int row = 0; row < unformattedInts.length - 1; row ++) {
            String lowerRow = "*"; // the row to print below this one

            System.out.print("*");

            for (int i = 0; i < unformattedInts[row].length - 1; i ++) {

                if (lengthOfInt(unformattedInts[row][i]) < maxDigits) {
                    System.out.print("0".repeat(maxDigits - (lengthOfInt(unformattedInts[row][i]))));
                }

                System.out.print(unformattedInts[row][i]);

                if (unformattedInts[row][i] == unformattedInts[row][i + 1]) {
                    System.out.print(" ");
                } else {
                    System.out.print("*");
                }

                if (unformattedInts[row][i] == unformattedInts[row + 1][i]) {
                    lowerRow += " ".repeat(maxDigits);
                    lowerRow += "*";
                } else {
                    lowerRow += "*".repeat(maxDigits + 1);
                }

            }

            if (lengthOfInt(unformattedInts[row][unformattedInts[row].length - 1]) < maxDigits) {
                System.out.print("0".repeat(maxDigits - (lengthOfInt(unformattedInts[row][unformattedInts[row].length - 1]))));
            }

            System.out.print(unformattedInts[row][unformattedInts[row].length - 1]);

            System.out.println("*");

            // doing last char

            if (unformattedInts[row][unformattedInts[row].length - 1] == unformattedInts[row + 1][unformattedInts[row].length - 1]) {
                lowerRow += " ".repeat(maxDigits);
                lowerRow += "*";
            } else {
                lowerRow += "*".repeat(maxDigits + 1);
            }

            System.out.println(lowerRow);
        }

        // doing last row

        System.out.print("*");

        for (int i = 0; i < unformattedInts[unformattedInts.length - 1].length - 1; i ++) {

            if (lengthOfInt(unformattedInts[unformattedInts.length - 1][i]) < maxDigits) {
                System.out.print("0".repeat(maxDigits - lengthOfInt(unformattedInts[unformattedInts.length - 1][unformattedInts[0].length - 1])));
            }

            System.out.print(unformattedInts[unformattedInts.length - 1][i]);

            if (unformattedInts[unformattedInts.length - 1][i] == unformattedInts[unformattedInts.length - 1][i + 1]) {
                System.out.print(" ");
            } else {
                System.out.print("*");
            }

        }

        if (lengthOfInt(unformattedInts[unformattedInts.length - 1][unformattedInts[unformattedInts.length - 1].length - 1]) < maxDigits) {
            System.out.print("0".repeat(maxDigits - lengthOfInt(unformattedInts[unformattedInts.length - 1][unformattedInts[unformattedInts.length - 1].length - 1])));
        }

        System.out.print(unformattedInts[unformattedInts.length - 1][unformattedInts[unformattedInts.length - 1].length - 1]);

        System.out.println("*");

        System.out.print("*".repeat(unformattedInts[0].length * maxDigits + unformattedInts[0].length + 1));

        System.out.println();
    }

    public static int lengthOfInt(int num) {
        return String.valueOf(num).length();
    }
}

Hope this helps :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...