【问题标题】:Printing an ArrayList of Objects as a table with non-fixed size in Java在 Java 中将对象的 ArrayList 打印为具有非固定大小的表
【发布时间】:2017-06-14 20:07:25
【问题描述】:

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

    =============================================
    | 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|

是否可以管理它以及如何管理?

【问题讨论】:

  • 遍历列表并计算每个属性的最大字符串长度(作为字符串)。然后您可以在开始显示之前设置所需的宽度。
  • 欢迎来到 Stack Overflow!请使用tour,环顾四周,并通读help center,尤其是How do I ask a good question?What topics can I ask about here?。从第二个链接:“要求家庭作业帮助的问题必须包括您迄今为止为解决问题所做的工作的摘要,以及您在解决问题时遇到的困难的描述。” - 即使没有作业。

标签: java printing size tabular


【解决方案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();
    }
    

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

然后使用这些最大宽度定义一个打印方法(例如)

public void printMyList(List<Person> aList, int[] maxColWidths)
{
     // Do your printing
}

这个question 应该帮助提供一种方法来格式化一个中心的字符串(如果需要)。

【讨论】:

    【解决方案2】:

    您需要事先设置每列的最大长度。然后您可以相应地调整表头并开始打印。遗憾的是,我不知道有任何其他方法可以将它漂亮地打印到控制台。

    【讨论】:

      【解决方案3】:

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

      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("=");
      
              }
      
          }
      
      }
      

      【讨论】:

      • 这只会将长度与第一列进行比较,不会找到每列的宽度。
      猜你喜欢
      • 2014-01-05
      • 2010-12-23
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 2018-05-03
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多