2014-11-25 72 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(); 
    } 
}