上对角线矩阵的主对角线以上元素

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

参见:示例图片

如何给高对角线以美学视图?需要在换行后移动空格。

4个回答

3
例如:
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();
}

1
很难理解你的实现方式。尽量避免给变量命名硬编码的名称。好的程序员编写易于人类理解的代码。
正如Sammy Larbi在Common Excuses Used To Comment Code中所说,如果你觉得你的代码太复杂了,需要注释才能理解,那么你的代码可能只是糟糕的。重写它,直到它不再需要注释。如果在这个过程的最后,你仍然觉得需要注释,那么请小心地添加注释。
因此,我建议采用以下实现方式:
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输出

enter image description here

输出,numbersOfSpacesBetwenElements = 5

enter image description here


0
我建议使用 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++) {

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接