Java lib在控制台上构建和打印表? [英] Java lib to build and print table on console?
本文介绍了Java lib在控制台上构建和打印表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有Java lib允许您以编程方式构建表,例如通过一个接一个地添加行/列,最后返回一个字符串(...可以在控制台上打印出来等等)?
Is there a Java lib out there which allows you to programatically build a table, e.g. by adding rows/cols one after each other, and finally returns a string (...that can be printed out on console or so)?
我找到的库并且只知道地址UI组件(JTable,Swing,AWT)...
The libs I have found and know address UI components (JTable, Swing, AWT) only...
推荐答案
我正在寻找类似的东西并结束我自己写的,我很乐意与你分享。我想要一些我可以使用的东西,比如
I was looking for something like this and ended up writing it myself, which I am happy to share with you. I wanted something that I can use like
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进行填充
using guava for padding
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 lib在控制台上构建和打印表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文