格式化二维数字数组

时间:2023-04-17
本文介绍了格式化二维数字数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

问题:
我有一个充满数字的二维数组.我必须以它显示的方式输出它: "*" 在具有不同值的邻居之间和 "" 如果值相同.示例:

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*
*********

我尝试了很多方法,例如创建另一个具有 [Nx2][Mx2] 大小或 System.out.format 的数组,但最终它从未格式化我喜欢.有什么建议可以解决这个问题吗?

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.

推荐答案

您可以遍历不是最后一行的每一行,然后遍历该数组中不是最后一行的每个字母,并检查它是否等于一个在右边,一个在底部.如果是,请打印相应的内容.

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.

类似的东西:

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

希望这会有所帮助:)

这篇关于格式化二维数字数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:DateTimeFormatter 自动更正无效(语法上可能)的日历日 下一篇:最好的 Eclipse 代码格式化程序?

相关文章

最新文章