有没有一种简单的方法可以在 Java 中将两列输出到控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/699878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an easy way to output two columns to the console in Java?
提问by James
As the title says, is there an easy way to output two columns to the console in Java?
正如标题所说,有没有一种简单的方法可以在 Java 中将两列输出到控制台?
I'm aware of \t
, but I haven't found a way to space based on a specific column when using printf.
我知道\t
,但在使用 printf 时,我还没有找到基于特定列的空间方法。
采纳答案by erickson
Use the width and precision specifiers, set to the same value. This will pad strings that are too short, and truncate strings that are too long. The '-' flag will left-justify the values in the columns.
使用宽度和精度说明符,设置为相同的值。这将填充太短的字符串,并截断太长的字符串。'-' 标志将使列中的值左对齐。
System.out.printf("%-30.30s %-30.30s%n", v1, v2);
回答by gtiwari333
i did it without using Formatter class as :
我在没有使用 Formatter 类的情况下做到了:
System.out.printf("%-10s %-10s %-10s\n", "osne", "two", "thredsfe");
System.out.printf("%-10s %-10s %-10s\n", "one", "tdsfwo", "thsdfree");
System.out.printf("%-10s %-10s %-10s\n", "onsdfe", "twdfo", "three");
System.out.printf("%-10s %-10s %-10s\n", "odsfne", "twsdfo", "thdfree");
System.out.printf("%-10s %-10s %-10s\n", "osdne", "twdfo", "three");
System.out.printf("%-10s %-10s %-10s\n", "odsfne", "tdfwo", "three");
and output was
和输出是
osne two thredsfe
one tdsfwo thsdfree
onsdfe twdfo three
odsfne twsdfo thdfree
osdne twdfo three
odsfne tdfwo three
回答by candied_orange
Late answer but if you don't want to hardcode the width, how about something that works like this:
迟到的答案,但如果您不想对宽度进行硬编码,那么像这样工作如何:
public static void main(String[] args) {
new Columns()
.addLine("One", "Two", "Three", "Four")
.addLine("1", "2", "3", "4")
.print()
;
}
And displays:
并显示:
One Two Three Four
1 2 3 4
Well all it takes is:
那么只需要:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Columns {
List<List<String>> lines = new ArrayList<>();
List<Integer> maxLengths = new ArrayList<>();
int numColumns = -1;
public Columns addLine(String... line) {
if (numColumns == -1){
numColumns = line.length;
for(int column = 0; column < numColumns; column++) {
maxLengths.add(0);
}
}
if (numColumns != line.length) {
throw new IllegalArgumentException();
}
for(int column = 0; column < numColumns; column++) {
int length = Math
.max(
maxLengths.get(column),
line[column].length()
)
;
maxLengths.set( column, length );
}
lines.add( Arrays.asList(line) );
return this;
}
public void print(){
System.out.println( toString() );
}
public String toString(){
String result = "";
for(List<String> line : lines) {
for(int i = 0; i < numColumns; i++) {
result += pad( line.get(i), maxLengths.get(i) + 1 );
}
result += System.lineSeparator();
}
return result;
}
private String pad(String word, int newLength){
while (word.length() < newLength) {
word += " ";
}
return word;
}
}
Since it won't print until it has all the lines, it can learn how wide to make the columns. No need to hard code the width.
由于它在所有行都打印完之前不会打印,因此它可以了解列的宽度。无需对宽度进行硬编码。