【问题标题】:elements of the matrix above the diagonal对角线上的矩阵元素
【发布时间】:2016-06-02 01:35:00
【问题描述】:
public class Main {
    public static void main(String[] args) {
        int array[][] = {
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
        };
        int i, j;
        {
            for (i = 0; i <5; i++) {
                for (j = 0+i; j < 5; j++) {
                    System.out.print(array[i][j] + " ");


                }
                System.out.println();
            }
        }
    }
}

见:example image

我怎样才能给出高对角线的审美观点? 换行后需要移动空间。

【问题讨论】:

    标签: java arrays matrix 2d


    【解决方案1】:

    例如:

    for (i = 0; i <5; i++) {
        for (j = 0; j < 5; j++) {
            if( j >= i ) {
                System.out.printf( "   ");
            else {
                System.out.printf( "%3d", array[i][j] );
            }
         }
         System.out.println();
    }
    

    【讨论】:

      【解决方案2】:

      很难理解您的实现。尽量避免硬编码给你的变量命名。优秀的程序员编写人类可以理解的代码。

      正如 Sammy Larbi 在Common Excuses Used To Comment Code 中所说,如果您的 觉得你的代码太复杂,没有 cmets 就无法理解,你的 代码可能很糟糕。重写它直到它不需要 cmets 不再。如果在努力结束时,你仍然觉得 cmets 必要时,请务必添加 cmets。小心点。

      所以,我建议这个实现:

      public static void printDiagonalMatrix(int array[][], int numberOfSpacesBetwenElements) {
              assert numberOfSpacesBetwenElements > 0 : "sizeOfSpaceBetwenNumbers should be > 0";
      
              int numRows = array.length;
              int numCols  = array[0].length;
              int tabulationSize = 1;
              int tabulationIncrement = numberOfSpacesBetwenElements + 1;
      
              String spacesBetwenElements = String.format("%" + numberOfSpacesBetwenElements + "s", "");
      
              StringBuilder out = new StringBuilder();
      
              for (int row = 0; row < numRows; row++) {
      
                  String tabulation = String.format("%" + tabulationSize + "s", "");
      
                  StringBuilder line = new  StringBuilder(tabulation);
                  for (int column = row; column < numCols; column++) {
                      line.append(array[row][column]).append(spacesBetwenElements);
                  }
                  line.append("\n");
      
                  out.append(line);
      
                  tabulationSize += tabulationIncrement;
              }
      
              System.out.print(out);
          }
      

      调用示例:

      int numberOfSpacesBetwenElements = 1;
      printDiagonalMatrix(array, numberOfSpacesBetwenElements);
      

      numberOfSpacesBetwenElements = 1 的输出

      numberOfSpacesBetwenElements = 5 的输出

      【讨论】:

        【解决方案3】:

        我会推荐一个 StringBuilder 和一个 for 循环到 append 足够的字符。这也比多次打印到控制台更有效。

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < i; j++) {
                sb.append("  ");
            }
            for (int j = i; j < 5; j++) {
                sb.append(array[i][j]).append(' ');
            }
            sb.append('\n');
        }
        System.out.print(sb.toString());
        

        请注意,这仅在 array 包含单个数字时有效。

        我还建议您在语句中定义 for 循环语句的整数索引。此外,您的代码中的 j = 0+i 也是不必要的。

        另一个建议,您可以通过获取数组的长度而不是硬编码来提高可维护性,例如

        for (int i = 0; i < array.length; i++) {
            for (int j = i; j < array[i].length; j++) {
        

        【讨论】:

          【解决方案4】:
          int array[][] = {
                  {2, 3, 4, 5, 6},
                  {2, 3, 4, 5, 6},
                  {2, 3, 4, 5, 6},
                  {2, 3, 4, 5, 6},
                  {2, 3, 4, 5, 6},
          };
          int i, j;
          
          StringBuilder prefix = new StringBuilder();
          
          for (i = 0; i < 5; i++) {
            prefix.append(String.format("%" + (i+1) + "s", ""));
            System.out.print(prefix.toString().substring(1));
            for (j = i; j < 5; j++) {
              System.out.print(array[i][j]+" ");
            }
            prefix.setLength(prefix.length() - i);
            System.out.println();
          }
          

          如果矩阵位数较多,则可以使用:

          int array[][] = {
                  {2, 34, 44, 555, 6},
                  {2, 34, 44, 555, 6},
                  {2, 34, 44, 555, 6},
                  {2, 34, 44, 555, 6},
                  {2, 34, 44, 555, 6},
          };
          int i, j;
          
          StringBuilder prefix = new StringBuilder();
          
          for (i = 0; i < 5; i++) {
            int elemLength = i > 0 ? ("" + array[i - 1][i - 1]).length() : 0;
            prefix.append(String.format("%" + (i + elemLength + 1) + "s", ""));
            System.out.print(prefix.toString());
            for (j = i; j < 5; j++) {
              System.out.print(array[i][j]+" ");
            }
            prefix.setLength(prefix.length() - (i + 1));
            System.out.println();
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-02-05
            • 1970-01-01
            • 1970-01-01
            • 2021-04-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多