I want to write a method toStringfor the Matrix class where I need to return a formatted string containing a matrix. The entries in the matrix are very different in length, so simply separating the entries with the help is TABnot a trick. Now I have the following:
public String toString(){
String str = "";
for(int i=0;i < _dim[0];i++){
for(int j=0;j < _dim[1];j++){
str += this.values[i][j] + "\t" + "\t";
}
str += "\n";
}
return str;
}
Which gives me something like this.
3.2004951E7 -1.591328E7 17839.0
-35882.0 17841.0 -20.0
1794.0 -892.0 1.0
Is there a way to print these correctly aligned without knowing in advance how long each record will be?
Thanks.
source
share