用于在控制台上构建和打印表的 Java 库?

标签 java

<分区>

是否有 Java 库允许您以编程方式构建表,例如通过一个接一个地添加行/列,最后返回一个字符串(...可以在控制台上打印出来)?

我发现和知道的库仅处理 UI 组件(JTable、Swing、AWT)...

最佳答案

我一直在寻找这样的东西,最后自己写了它,我很高兴与你分享。我想要一些我可以使用的东西,比如

ConsoleStringTable table= new ConsoleStringTable();
table.addString(0, 0, "AveryVeryVeryLongWord");
table.addString(0, 1, "AnotherWord");
table.addString(1, 0, "Short");
table.addString(1, 1, "Fast");
System.out.println(table.toString());

哪些输出

AveryVeryVeryLongWord AnotherWord
Short                 Fast       

实现

使用 Guava 做填充

package util;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Strings;

public class ConsoleStringTable {

    private class Index {

        int _row, _colum;

        public Index (int r, int c) {
            _row= r;
            _colum= c;
        }

        @Override
        public boolean equals (Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Index other= (Index) obj;
            if (_colum != other._colum)
                return false;
            if (_row != other._row)
                return false;
            return true;
        }

        @Override
        public int hashCode () {
            final int prime= 31;
            int result= 1;
            result= prime * result + _colum;
            result= prime * result + _row;
            return result;
        }
    }

    Map<Index, String> _strings= new HashMap<ConsoleStringTable.Index, String>();
    Map<Integer, Integer> _columSizes= new HashMap<Integer, Integer>();

    int _numRows= 0;
    int _numColumns= 0;

    public void addString (int row, int colum, String content) {
        _numRows= Math.max(_numRows, row + 1);
        _numColumns= Math.max(_numColumns, colum + 1);

        Index index= new Index(row, colum);
        _strings.put(index, content);

        setMaxColumnSize(colum, content);
    }

    private void setMaxColumnSize (int colum, String content) {
        int size= content.length();
        Integer currentSize= _columSizes.get(colum);
        if (currentSize == null || (currentSize != null && currentSize < size)) {
            _columSizes.put(colum, size);
        }
    }

    public int getColumSize (int colum) {
        Integer size= _columSizes.get(colum);
        if (size == null) {
            return 0;
        } else {
            return size;
        }
    }

    public String getString (int row, int colum) {
        Index index= new Index(row, colum);
        String string= _strings.get(index);
        if (string == null) {
            return "";
        } else {
            return string;
        }
    }

    public String getTableAsString (int padding) {
        String out= "";
        for (int r= 0; r < _numRows; r++) {
            for (int c= 0; c < _numColumns; c++) {
                int columSize= getColumSize(c);
                String content= getString(r, c);
                int pad= c == _numColumns - 1 ? 0 : padding;
                out+= Strings.padEnd(content, columSize + pad, ' ');
            }
            if (r < _numRows - 1) {
                out+= "\n";
            }
        }
        return out;
    }

    @Override
    public String toString () {
        return getTableAsString(1);
    }

}

关于用于在控制台上构建和打印表的 Java 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5551186/

相关文章:

java - 粘性前台服务无法重新启动并出现 "process is bad"错误

java - 带身份验证的 HTTP Get 请求 Android

java - 使用SDK进行服务开发时无法从S/4系统中检索数据

java - 对 JSONArray 中的每一行使用相同的 ResultSet

java - 闰秒持续时间

java - 在大字符串中搜索一组关键字

java - 这有什么问题会返回以下错误?

java - 如何在 Struts 2 中获取 ActionForm?

java - 如何从java中的同一位置创建多个对象

java - jsp 文本字段绑定(bind)