对角线上方矩阵的元素

时间:2016-02-20 11:59:23

标签: java arrays matrix 2d

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

如何才能给出高对角线的美学观点? 换线后需要换空间。

4 个答案:

答案 0 :(得分:2)

例如:

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 :(得分: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

答案 2 :(得分: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++) {

答案 3 :(得分: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();
}