Java的:具有非常不同的条目长度为格式的字符串返回二维数组 [英] Java: return 2D Array with very varying entry lengths as formatted string
问题描述
我想写一个Matrix类,我需要返回一个格式化字符串,其中包含矩阵中的的toString
方法。在矩阵中的条目的长度很不同的,所以只是分离了设置页
的项不会的伎俩。现在,我有以下几点:
公共字符串的toString(){
字符串str =;
对(INT I = 0; I&下; _dim [0];我++){
为(中间体J = 0; J&下; _dim [1]; J ++){
STR + = this.values [I] [J] +\\ t+\\ t的;
}
STR + =\\ n;
}
返回海峡;
}
这给了我这样的事情。
3.2004951E7 -1.591328E7 17839.0
-35882.0 17841.0 -20.0
1794.0 -892.0 1.0
有没有一种方法来打印这些正确对齐不知道提前多久每个条目将是?
感谢。
有没有一种方法来打印这些正确对齐不知道的
推进各条目要去多久呢?
块引用>我不认为有任何内置的库函数可用它可以做这样的事情
为了我们。我们必须使我们自己的格式,例如,使用:% - XS
:
- 创建一个一维数组:
了maxWidth [COL]
来找到每个列的最大宽度:列大小等于二维数组的列数。- 访问二维数组找到每一列
的最大宽度- 创建使用每一列
% - 格式化字符串+了maxWidth [COL] +S
,其中-
在%
指定格式为左对齐。- 请查看格式化数字打印输出了解详细信息。
样品code:
字符串数据[] [] = {{3.2004951E7 ++ -1.591328E7,17839.0+},
{-35882.0 +,-17841.0+,-20.0+}};
INT COL =数据[0]。长度;
INT排= data.length; INT了maxWidth [] =新的INT [COL]; 对于(字符串[] rowD:数据)
的for(int i = 0; I<山坳,我++)
{
如果(了maxWidth [1] - ; rowD [I]。长度())
了maxWidth [I] = rowD [I]。长度();
} 字符串格式=; 对于(INT X:了maxWidth)
格式+ =% - +(X + 2)+的; 格式+ =%N的; 通信System.err.println(格式); 对于(字符串[] rowD:数据)
{
System.out.printf(格式,rowD);
}输出:
3.2004951E7 -1.591328E7 17839.0
-35882.0 -17841.0 -20.0I want to write a
toString
method for a Matrix class where I need to return a formatted string which contains the matrix. The entries in the matrix are very different in length so just separating the entries with aTAB
doesn't the trick. Right 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 properly aligned without knowing in advance how long each entry is going to be?
Thanks.
解决方案Is there a way to print these properly aligned without knowing in advance how long each entry is going to be?
I don't think there is any built-in library function available which can do such things for us. We will have to make our own formatting such as, using:
%-xs
:
- Create an one dimensional array:
maxWidth[col]
to find the maximum width of each column: the column size is equal to the number of column of the 2D array.- Visit the 2D array to find the maximum width of each column
- create the formatting string for each column using
"%-"+maxWidth[col]+"s "
, where-
after the%
specifies formatting for left-justified.- Please check out the formatting Numeric Print Output for more details.
The sample code:
String data[][] = {{3.2004951E7+"" , -1.591328E7+"", 17839.0+"" }, {-35882.0 +"" , -17841.0+"", -20.0+"" }}; int col = data[0].length; int row = data.length; int maxWidth[] = new int[col]; for(String[] rowD : data) for(int i=0; i< col; i++) { if(maxWidth[i] < rowD[i].length()) maxWidth[i] = rowD[i].length(); } String format = ""; for(int x : maxWidth) format += "%-"+(x+2)+"s "; format +="%n"; System.err.println(format); for(String[] rowD : data) { System.out.printf(format, rowD); }
Output:
3.2004951E7 -1.591328E7 17839.0 -35882.0 -17841.0 -20.0
这篇关于Java的:具有非常不同的条目长度为格式的字符串返回二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!