将对象的ArrayList打印为Java中具有非固定大小的表

时间:2017-06-14 20:07:26

标签: java printing size tabular

我有一个对象列表,我需要将其打印为一个表,其中第一行是标题,后面的每一行是一个对象,行中的每一列代表一个属性。我需要表格根据每个字段中文本的大小调整其大小。例如,我需要这样的东西:

    =============================================
    | First Name  |   Last Name    |     Age    |
    =============================================
    |Person's name|Person's surname|Person's age|

如果字段"名字"中的文字改变大小变得更大,像这样:

    =======================================================
    |       First Name      |   Last Name    |     Age    |
    =======================================================
    |Person's very long name|Person's surname|Person's age|

是否有可能管理这个以及如何管理?

3 个答案:

答案 0 :(得分:1)

我会假设你有类似Person这样的对象

public class Person
{
     public String fName;
     public String lName;
     public String age;
}
  1. 实现你自己的列表,它会在你添加元素时跟踪宽度,就像(非常粗略的例子)

    public class MyList<T extends Person> extends ArrayList<T>
    {
        public int[] colWidths = new int[3];
    
        @Override
        public boolean add(T e)
        {
             colWidths[0] = (colwidths[0] > e.fName.length()) ? colwidths[0] : e.fName.length();
             colWidths[1] = (colwidths[1] > e.lName.length()) ? colwidths[1] : e.lName.length();
             colWidths[2] = (colwidths[2] > e.age.length()) ? colwidths[2] : e.age.length();
    
             return super.add(e);
        }
    }
    
  2. 迭代列表以计算最大宽度

    public int[] colWidths = new int[3];
    for(Person p : yourList)
    {
             colWidths[0] = (colwidths[0] > p.fName.length()) ? colwidths[0] : p.fName.length();
             colWidths[1] = (colwidths[1] > p.lName.length()) ? colwidths[1] : p.lName.length();
             colWidths[2] = (colwidths[2] > p.age.length()) ? colwidths[2] : p.age.length();
    }
    

    第二种方法的明显缺点是你需要两次迭代列表。

  3. 然后使用这些最大宽度(例如)

    定义打印方法
    public void printMyList(List<Person> aList, int[] maxColWidths)
    {
         // Do your printing
    }
    

    如果需要,此question应该有一种格式化字符串格式的方法。

答案 1 :(得分:0)

您需要预先确定每列的最大长度。然后,您可以相应地调整表头并开始打印。可悲的是,我不知道如何将其打印到控制台上。

答案 2 :(得分:0)

我同意上面的Rudy Velthuis。代码应该迭代以获得String的最大值,然后在文本周围绘制框。应该是这样的:

import java.util.Scanner;

public class GettingBiggerName {

    static String firstName, secondName, thirdName; // testing with just 3 Strings that will be inserted in an Array

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter first name: ");
        firstName = in.nextLine();

        System.out.print("Enter second name: ");
        secondName = in.nextLine();

        System.out.print("Enter third name: ");
        thirdName = in.nextLine();

        System.out.println();

        String[] arrayOne = { firstName, secondName, thirdName }; // Created the array with the 3 strings for testing purpose

        int count=0; int progress = 0;
        for (int i = 0; i < arrayOne.length; i++) { // iterating to get the biggest length of the Strings inside the array

            if (arrayOne[i].length() > arrayOne[progress].length()) {
                count = arrayOne[i].length();
                progress++;
            }

            else {
                count = arrayOne[progress].length();
            }

        }

        System.out.println("Printing the header of the box: ");

        // printing the box to fit the length of the biggest String inside the array
        for (int i = 0; i < count; i++) {
            System.out.print("=");

        }

    }

}