2014-11-25 109 views
1

我無法弄清楚如何從arraylist格式化我的打印, 我只是不知道該怎麼做。任何提示或片段可以幫助嗎?謝謝搞清楚如何格式化打印

protected void printTable(ArrayList<double[]> table) 
{ 
    String s = ""; 
    System.out.printf("\n_____________________________________________________\n"); 
    System.out.printf("\n x\t f[]\t f[,] f[,,] f[,,,] "); 
    System.out.printf("\n_____________________________________________________\n"); 
    for(int a = 0; a < table.size(); a++) 
    { 
     System.out.printf("%.3s", s); 
     for(double c : table.get(a)) 
     { 
      System.out.printf("%.3f\t " , c); 
     } 
     System.out.println(); 
    } 
} 

它這樣印刷的時刻:

_____________________________________________________ 
    x  f[]  f[,] f[,,] f[,,,] 
    _____________________________________________________ 
    1.000 1.500 0.000 2.000 
    3.000 3.250 3.000 1.670 
    0.500 0.167 -0.665 
    0.333 -1.663 
    -1.997 

我怎麼得到它呢?

_____________________________________________________ 

x  f[]  f[,] f[,,] f[,,,] 
_____________________________________________________ 
1.000 3.000 0.500 0.333 -1.997 
1.500 3.250 0.167 -1.663 
0.000 3.000 -0.665 
2.000 1.670 

回答

2

可以使用證明你列 - 標誌

System.out.printf("%-.3f\t " , c); 

,並且可以使用指定的寬度(切換到任何你想要的10寬)

System.out.printf("%-10.3f " , c); 

我會建議您刪除\t,而是使用寬度和精度標誌(上例中的10.3)控制寬度

您可以控制打印的使用2維數組,而不是數組的ArrayList

double table [][]; 
+0

謝謝,但我該如何旋轉它? – 2014-11-25 08:42:31

+0

除了數組的ArrayList,您可以使用2維數組double [] []。然後你可以更明確地控制你的迭代器 – CocoNess 2014-11-25 09:22:29

0
protected void printTable(ArrayList<double[]> table) 
{ 
    String s = ""; 
    System.out.printf("\n_____________________________________________________\n"); 
    System.out.printf("\n x\t f[]\t f[,] f[,,] f[,,,] "); 
    System.out.printf("\n_____________________________________________________\n"); 
    for(int i = 0; i < table.size(); i++) { 
     for(int a = 0; a < table.size(); a++) 
     { 
      if (table.get(a).length > i) { 
       System.out.printf("%.3f\t " , table.get(a)[i]); 
      } 
     } 
     System.out.println(); 
    } 
} 
0

下面的代碼旋轉矩陣爲您的方案的命令,但我只查了NxN矩陣,你應該能夠解決問題。

public void printTable(ArrayList<double[]> table) 
{ 
    String s = ""; 
    System.out.printf("\n_____________________________________________________\n"); 
    System.out.printf("\n x\t f[]\t f[,] f[,,] f[,,,] "); 
    System.out.printf("\n_____________________________________________________\n"); 
    int i =0; 

    for(int a = 0; a < table.size(); a++) 
    { 
     System.out.printf("%.3s", s); 
     for(int j= 0;j<table.size();j++){ 
      double[] d = table.get(j); 

      for(int k =a;k<=a;k++){ 
       System.out.printf("%.3f\t " , d[k]); 
      } 
     } 
     System.out.println(); 
    } 
}