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