java格式的2d数字数组

wa7juj8i  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(302)

问题:
我有一个二维数组,里面充满了数字。我必须以一种显示的方式输出它: "*" 不同价值观的邻居之间 " " 如果值相同。例子:


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

我尝试过很多方法,比如用 [Nx2][Mx2] 大小或 System.out.format 但最终它的格式从来都不是我喜欢的。有什么建议我怎么解决这个问题?

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

它显示类似于我想要的结果,但它的格式与表中的格式不同。

mspsb9vt

mspsb9vt1#

您可以组成一个字符串数组,该数组由第一个数组及其相邻元素(即)中的数字组成。e。空格和星号。如有必要,用前导零和适当的相邻元素计数格式化它们。对于每一行,创建两个字符串数组,即。e。具有水平邻域的元素和具有垂直邻域的中间数组,以及前后行的星号。然后将它们垂直展平,并将行中的元素水平连接成一个字符串:

int[][] arr1 = {
        {1, 1, 3, 3, 4, 6, 5, 6, 8, 75},
        {2, 2, 2, 3, 4, 5, 5, 5, 8, 8},
        {3, 3, 5, 5, 6, 7, 7, 5, 2, 8}};

// numbers format, digits count
int s = 2;

// formatted array representation
// with neighbouring elements
String[] arr2 = IntStream
        // iterate over indices of the rows of the
        // array, plus leading row of asterisks
        .range(-1, arr1.length)
        // for each row create two arrays of strings:
        // horizontal neighbours and vertical neighbours
        .mapToObj(i -> {
            // assume the lengths of the rows are identical
            int length = arr1[0].length * s * 2 - s + 2;
            String[] asterisks = new String[length];
            Arrays.fill(asterisks, "*");
            if (i == -1)
                // leading row of asterisks
                return Stream.of(null, asterisks);
            else
                // iterate over indices of the elements of the rows,
                // add asterisks between neighbours with different
                // values and spaces if values are the same
                return Stream.of(IntStream.range(0, arr1[i].length)
                            // horizontal neighbours
                            .mapToObj(j -> {
                                // formatted representation
                                // of the element of the row,
                                // with a leading zeros if necessary
                                String val = String.format(
                                        "%0" + s + "d", arr1[i][j]);
                                if (j == 0)
                                    // leading asterisk
                                    val = "*" + val;
                                if (j == arr1[i].length - 1)
                                    // trailing asterisk
                                    val += "*";
                                else
                                    // neighbour element
                                    val += arr1[i][j] == arr1[i][j + 1] ?
                                            " ".repeat(s) : "*".repeat(s);
                                return val;
                            }).toArray(String[]::new),
                        // second array
                        (i == arr1.length - 1) ?
                            // trailing row of asterisks
                            asterisks :
                            // vertical neighbours
                            IntStream.range(0, arr1[i].length)
                                .mapToObj(j -> {
                                    String val = "";
                                    if (j == 0)
                                        // leading asterisk
                                        val = "*";
                                    // neighbour element
                                    val += (arr1[i][j] == arr1[i + 1][j] ?
                                            " ".repeat(s) : "*".repeat(s));
                                    if (j == arr1[i].length - 1)
                                        // trailing asterisk
                                        val += "*";
                                    else
                                        // intermediate asterisks
                                        val += "*".repeat(s);
                                    return val;
                                }).toArray(String[]::new));
        }).flatMap(Function.identity())
        // skip first null
        .skip(1)
        // Stream<Stream<String>>
        .map(Arrays::stream)
        // join each row into one string
        .map(stream -> stream.collect(Collectors.joining()))
        .toArray(String[]::new);

// output
Arrays.stream(arr2).forEach(System.out::println);

输出:


****************************************
* 01  01**03  03**04**06**05**06**08**75*
********************************
* 02  02  02**03**04**05  05  05**08  08*
************************************
* 03  03**05  05**06**07  07**05**02**08*
****************************************
lmvvr0a8

lmvvr0a82#

通过首先遍历数组中的数组数,然后遍历每个数组来遍历2d数组。
在各个数组中,检查这是否是数组中的第一项。如果是,请打印 * . 然后检查前一个是否相等等,。
对于[具有相同项]的相邻行之间的“leave”,我们可以将星行存储在stringbuilder中,并在末尾打印出来。

int[][] arr = {{1, 1, 3, 4},{2, 2, 3, 4}};
int lineLength = arr[0].length*2+1;
for(int i = 0; i < lineLength-1; i++) {
    System.out.print("*");
}
System.out.println();
for(int i = 0; i < arr.length; i++) {
    int[] current = arr[i];
    int before = 0;
    StringBuilder str = new StringBuilder();
    for(int j = 0; j < current.length; j++) {
        int rn = current[j];
        if(j==0) {
            System.out.print("*");
            System.out.print(rn);

        }else {
            if(before==rn) {
                System.out.print(" ");
                System.out.print(rn);
            }else {
                System.out.print("*");
                System.out.print(rn);
            }
        }
        if(i!=arr.length-1&&arr[i+1][j]==rn) {
            str.append("* ");
        }else {
            str.append("**");
        }
        before = rn;
    }
    if(i!=arr.length-1) {
        System.out.println();
        System.out.println(str.toString());
    }
}
System.out.println();
for(int i = 0; i < lineLength-1; i++) {
    System.out.print("*");
}

打印内容:


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

这花了太多时间

db2dz4w8

db2dz4w83#

可以循环遍历数组中不是最后一行的每一行,然后遍历数组中不是最后一行的每一个字母,并检查它是否等于右边的一个和下面的一个。如果是,打印合适的东西。
大致如下:

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

希望这有帮助:)

相关问题