2016-02-20 44 views
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(); 
      } 
     } 
    } 
} 

见上文:example image对角线

如何我可以给高对角线的审美观? 换行后需要移位空格。

回答

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

很难理解你的实现。尽量避免硬编码赋予变量名称。好的程序员编写人类可以理解的代码。

由于萨米·拉尔在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
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(); 
}