How to print a table of arrays?

后端 未结 3 1516
北荒
北荒 2021-01-26 01:44

I\'m here to see if anyone would be able to help out with the problem.

I\'m trying to print a table out which would look something like this

                    


        
3条回答
  •  感动是毒
    2021-01-26 02:24

    I know this post is very old but, because I often fall in this question, probably because on internet it is watched by many people, I finally wanted to publish my aswer to it.

    I wrote a very simple class that uses the String.format method to format a generic table of objects:

    public class TableFormatter {
        private int columns;
        private List cells = new LinkedList<>();
        private int minSpacesBetweenCells = 4;
        private boolean alignLeft = true;
        private int maxLength = 0;
    
        public TableFormatter(int columns) {
            this.columns = columns;
        }
    
        public TableFormatter insert(Object... cells) {
            for (Object content : cells) {
                String cell = content.toString();
                maxLength = Math.max(maxLength, cell.length());
                this.cells.add(cell);
            }
            return this;
        }
    
        public TableFormatter setMinSpacesBetweenCells(int minSpacesBetweenCells) {
            this.minSpacesBetweenCells = minSpacesBetweenCells;
            return this;
        }
    
        public TableFormatter alignCellsToRight() {
            this.alignLeft = false;
            return this;
        }
    
        public TableFormatter alignCellsToLeft() {
            this.alignLeft = true;
            return this;
        }
    
        @Override
        public String toString() {
            String format = "%";
            if (alignLeft)
                format += "-";
            format += maxLength + "s";
    
            String spaces = new String(new char[minSpacesBetweenCells]).replace("\0", " ");
    
            StringBuilder sb = new StringBuilder();
    
            int row = 0;
            int currentColumn = 0;
            for (String cell : cells) {
                if (currentColumn == 0) {
                    if (row > 0)
                        sb.append("\n");
                } else {
                    sb.append(spaces);
                }
    
                sb.append(String.format(format, cell));
    
                currentColumn = (currentColumn + 1) % columns;
                if (currentColumn == 0)
                    row++;
            }
    
            return sb.toString();
        }
    
        public static void main(String[] args) {
            TableFormatter tableFormatter = new TableFormatter(3);
    
            tableFormatter.insert("column1", "column2", "column3");
            tableFormatter.insert("e11", 12, "e13");
            tableFormatter.insert("e21", "e22", 23);
            tableFormatter.insert(3.1d, "e32", "e33");
            tableFormatter.insert("e41", "e42", true);
    
            System.out.println(tableFormatter);
        }
    }
    

    See the main method at the bottom of the class. It produces the following output:

    column1    column2    column3
    e11        12         e13    
    e21        e22        23     
    3.1        e32        e33    
    e41        e42        true   
    

    I hope this will be useful to next visitors of this post.

提交回复
热议问题