有没有一种简单的方法可以用Java将两列输出到控制台?

有没有一种简单的方法可以用Java将两列输出到控制台?,java,Java,正如标题所说,有没有一种简单的方法可以用Java将两列输出到控制台 我知道\t,但在使用printf时,我没有找到一种基于特定列的空格的方法。使用宽度和精度说明符,设置为相同的值。这将填充太短的字符串,并截断太长的字符串。“-”标志将左对齐列中的值 System.out.printf("%-30.30s %-30.30s%n", v1, v2); 我这样做时没有使用Formatter类,因为: System.out.printf("%-10s %-10s %-10s\n", "osne",

正如标题所说,有没有一种简单的方法可以用Java将两列输出到控制台


我知道
\t
,但在使用printf时,我没有找到一种基于特定列的空格的方法。

使用宽度和精度说明符,设置为相同的值。这将填充太短的字符串,并截断太长的字符串。“-”标志将左对齐列中的值

System.out.printf("%-30.30s  %-30.30s%n", v1, v2);

我这样做时没有使用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");
产量是

osne       two        thredsfe  
one        tdsfwo     thsdfree  
onsdfe     twdfo      three     
odsfne     twsdfo     thdfree   
osdne      twdfo      three     
odsfne     tdfwo      three     

回答晚了,但如果您不想硬编码宽度,那么可以采用如下方式:

public static void main(String[] args) {
    new Columns()
        .addLine("One", "Two", "Three", "Four")
        .addLine("1", "2", "3", "4")
        .print()
    ;
}
并显示:

1234
1   2   3     4    
那么所需要的就是:

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;
    }
}
import java.util.ArrayList;
导入java.util.array;
导入java.util.List;
公共类列{
列表行=新的ArrayList();
List MaxLength=新的ArrayList();
int numColumns=-1;
公共列添加行(字符串…行){
if(numColumns==-1){
numColumns=line.length;
for(int column=0;column

因为它在拥有所有行之前不会打印,所以它可以了解列的宽度。无需硬编码宽度。

有关Java字符串格式的更多详细信息,请参见
%-30.30s
%-30s
?@JohnRPerry.30,最大字段宽度为30。较长的值将被截断。但对于此类语句,我们无法应用该语句<代码>str+=“\t”+item.getName()+”[“+item.getValue()+”]“+”\t”+item.getDescription()+“\n”
对于这种情况,我们应该怎么做?@chamzz.dot使用
String.format()
而不是串接字符串。它看起来像是在一个循环中使用的,这个循环可能具有二次时间复杂度(换句话说,这很糟糕)。相反,将
str
a
StringBuilder
置于循环外,并将
append()
作为每个
String.format()
调用在循环内的结果。我对Java有点陌生,对方法之间的通信方式感到困惑
pad()
System.lineSeparator()
和此行
maxLength.set(i,Math.max(maxLength.get(i),line[i].length())
这到底是做什么的?很抱歉问了这么多问题。我只是不喜欢使用我不知道它到底是如何工作的代码。@好的。更好吗?这正是我想要的。虽然在这里使用
StringBuilder
而不是字符串串联不是更好吗?我知道这个解决方案的目的不是t来解决这个问题,但还是最好在这里使用
StringBuilder
,因为字符串串联非常昂贵。