如何打印此格式

时间:2014-07-18 22:14:12

标签: java stringbuilder system.out

我想打印这种格式的人,每个人都在一个栏目中,每个人都用空格或标签分隔,即

约翰史密斯  男性男  01 02

如何修改toString()以打印出漂亮的格式?目前,每个字段和每个人都打印在一行中。

public class Test {

public static void main(String[] args) {

    ArrayList<Person> persons = new ArrayList<Person>();

    persons.add(new Person("John", "Male", "01"));
    persons.add(new Person("Smith", "Male", "02"));
    persons.add(new Person("Jack", "Male", "03"));
    persons.add(new Person("Mary", "Female", "04"));
    persons.add(new Person("Alice", "Feamle", "05"));

    for(Person p : persons){
        System.out.print(p + " ");
    }
}

static class Person{

    private String name;
    private String sex;
    private String ID;

    public Person(String n, String s, String id){
        name = n;
        sex = s;
        ID = id;
    }

    public String toString(){

        StringBuilder sb = new StringBuilder();

        sb.append(ID + "\n");
        sb.append(name + "\n");
        sb.append(sex + "\n");

        return sb.toString();
    }
}
}

我真正想要的是这种格式。我有一个优先级队列,为了调试,我需要以这种格式打印出优先级队列的内容。

优先级队列跟踪。

Here are the contents of our priority queue (sorted by priority) just before dequeueing each node when using the Manhattan priority function on puzzle04.txt.

    Step 0:    priority  = 4
               moves     = 0
               manhattan = 4
               3            
                0  1  3     
                4  2  5     
                7  8  6     

    Step 1:    priority  = 4    priority  = 6
               moves     = 1    moves     = 1
               manhattan = 3    manhattan = 5
               3                3            
                1  0  3          4  1  3     
                4  2  5          0  2  5     
                7  8  6          7  8  6     

    Step 2:    priority  = 4    priority  = 6    priority  = 6
               moves     = 2    moves     = 1    moves     = 2
               manhattan = 2    manhattan = 5    manhattan = 4
               3                3                3            
                1  2  3          4  1  3          1  3  0     
                4  0  5          0  2  5          4  2  5     
                7  8  6          7  8  6          7  8  6     

    Step 3:    priority  = 4    priority  = 6    priority  = 6    priority  = 6    priority  = 6
               moves     = 3    moves     = 3    moves     = 2    moves     = 3    moves     = 1
               manhattan = 1    manhattan = 3    manhattan = 4    manhattan = 3    manhattan = 5
               3                3                3                3                3            
                1  2  3          1  2  3          1  3  0          1  2  3          4  1  3     
                4  5  0          4  8  5          4  2  5          0  4  5          0  2  5     
                7  8  6          7  0  6          7  8  6          7  8  6          7  8  6     

    Step 4:    priority  = 4    priority  = 6    priority  = 6    priority  = 6    priority  = 6    priority  = 6
               moves     = 4    moves     = 3    moves     = 4    moves     = 2    moves     = 3    moves     = 1
               manhattan = 0    manhattan = 3    manhattan = 2    manhattan = 4    manhattan = 3    manhattan = 5
               3                3                3                3                3                3            
                1  2  3          1  2  3          1  2  0          1  3  0          1  2  3          4  1  3     
                4  5  6          0  4  5          4  5  3          4  2  5          4  8  5          0  2  5     
                7  8  0          7  8  6          7  8  6          7  8  6          7  0  6          7  8  6     

6 个答案:

答案 0 :(得分:1)

查看String.Format方法。我认为这正是您正在寻找的格式化您返回的字符串。这是文档的String.Format

答案 1 :(得分:1)

我建议您为get类使用Person方法,以便在遍历每个人时,您可以将此人的每个属性添加到字符串中,然后稍后显示。像这样:

import java.util.ArrayList;

public class PersonTest {

    public static void main(String[] args) {

        ArrayList<Person> persons = new ArrayList<Person>();

        persons.add(new Person("John", "Male", "01"));
        persons.add(new Person("Smith", "Male", "02"));
        persons.add(new Person("Jack", "Male", "03"));
        persons.add(new Person("Mary", "Female", "04"));
        persons.add(new Person("Alice", "Feamle", "05"));

        // Strings to display at the end of the iterations
        StringBuilder names = new StringBuilder(), sexes = new StringBuilder(), ids = new StringBuilder();

        for(Person p : persons) {
            // tab after each property
            names.append(p.getName() + "\t");
            sexes.append(p.getSex() + "\t");
            ids.append(p.getId() + "\t");
        }

        System.out.println(names);
        System.out.println(sexes);
        System.out.println(ids);
    }

    static class Person {

        private String name;
        private String sex;
        private String ID;

        public Person(String n, String s, String id) {
            name = n;
            sex = s;
            ID = id;
        }

        // getter methods to get the properties of this person
        public String getName() {
            return name;
        }
        public String getSex() {
            return sex;
        }
        public String getId() {
            return ID;
        }
    }
}

输出是这样的:

John    Smith   Jack    Mary    Alice   
Male    Male    Male    Female  Feamle  
01      02      03      04      05  

答案 2 :(得分:0)

您无法通过覆盖toString()方法来执行此操作。你必须编写一些字符串util方法,它将占用List个人并构建列表的字符串表示并返回它。完成填充Person个对象后,请调用此util方法。

要构建字符串,您可以使用StringBuilder对象。

Util方法看起来像这样

StringBuilder sb = new StringBuilder();
for(Person p : persons){
    sb.append(p.getName()+"\t");
}
sb.append("\n")
for(Person p : persons){
    sb.append(p.getSex() + "\t");
}
sb.append("\n")
for(Person p : persons){
    sb.append(p.getID() + "\t");
}

答案 3 :(得分:0)

这样做的基本方法是:

public class Test {

public static void main(String[] args) {

    ArrayList<Person> persons = new ArrayList<Person>();

    persons.add(new Person("John", "Male", "01"));
    persons.add(new Person("Smith", "Male", "02"));
    persons.add(new Person("Jack", "Male", "03"));
    persons.add(new Person("Mary", "Female", "04"));
    persons.add(new Person("Alice", "Feamle", "05"));

    StringBuilder sb = new StringBuilder();
    for(Person p : persons){
        sb.append(p.getName() + " ");
    }
    sb.append("\n");
    for(Person p : persons){
        sb.append(p.getSex() + " ");
    }
    sb.append("\n");
    for(Person p : persons){
        sb.append(p.getID() + " ");
    }
    sb.append("\n");
    System.out.print(sb.toString());
}

static class Person{

    private String name;
    private String sex;
    private String ID;

    public Person(String n, String s, String id){
        name = n;
        sex = s;
        ID = id;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public String getID() {
        return ID;
    }
}

答案 4 :(得分:0)

我认为会是:

for(int i = 0; i < Persons.size()){
        System.out.print(Persons.get(i).name + " ");
}

答案 5 :(得分:0)

很快就会有一些人受到限制:

public class Test {
    public static void main(String[] args) {

    ArrayList<Person> persons = new ArrayList<Person>();

    persons.add(new Person("John", "Male", "01"));
    persons.add(new Person("Smith", "Male", "02"));
    persons.add(new Person("Jack", "Male", "03"));
    persons.add(new Person("Mary", "Female", "04"));
    persons.add(new Person("Alice", "Feamle", "05"));

    StringBuilder name = new StringBuilder();
    StringBuilder sex = new StringBuilder();
    StringBuilder ID = new StringBuilder();
    String delimeter = "";

   for(Person p : persons){
       name.append(delimeter).append(p.name);
       sex.append(delimeter).append(p.sex);
       ID.append(delimeter).append(p.ID);
       delimeter = "\t";
   }

   System.out.println(name);
   System.out.println(sex);
   System.out.println(ID);
}

输出John Smith Jack Mary Alice Male Male Male Female Feamle 01 02 03 04 05

如果名称很长并且打破了对齐,可能会引起一些额外的担忧。