I'm having trouble figuring out how to format my printing from arraylist, I just don't know how to do it. Any tips or snippets that could help? Thank you
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();
}
}
its printing like this at the moment:
_____________________________________________________
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
how do I get it to do?
_____________________________________________________
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
You can justify your columns using - flag
System.out.printf("%-.3f\t " , c);
And you can specify the width using (change the 10 width to whatever you want)
System.out.printf("%-10.3f " , c);
I would suggest that you remove the \\t
and rather control the width using the width and precision flags ( 10.3
in above example)
You can control the order of the printing using a 2 dimensional array instead of an ArrayList of array
double table [][];
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();
}
}
The code below rotates matrix for your scenario, but I have only checked for a nXn matrix, you should be able to work your way out of it.
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();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.