下你的榜樣,這樣的事情應該工作
String[] columns = {"applianc", " around", " autonom", " averag", " bath", " bedroom", " car", " charg", " drive", " eat", " home", " hour", " hous", " includ", " kilomet", " kitchen", " lap", " larg", " live", " mile", " over", " per", " pet", " road", " room", " second", " sedan", " size", " test", " up", " updat", " veri", " water"};
int[][] matrix = {{0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 1, 0, 0, 1, 0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0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, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 0, 0, 2, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 1}};
System.out.print(String.format("%" + ("sentence ".length() + ("" + matrix.length).length() + 2) + "s"," "));
for(int i=0; i<columns.length; i++){
System.out.print(columns[i]);
}
System.out.println("");
for(int i=0; i<matrix.length; i++) {
System.out.print("sentence " + String.format("%" + ("" + matrix.length).length() + "S", (i + 1)) + ": ");
for(int j=0; j<matrix[i].length; j++){
System.out.print(String.format("%" + columns[j].length() + "s", matrix[i][j]));
}
System.out.println("");
}
輸出:
applianc around autonom averag bath bedroom car charg drive eat home hour hous includ kilomet kitchen lap larg live mile over per pet road room second sedan size test up updat veri water
sentence 1: 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0
sentence 2: 0 0 0 0 0 0 1 0 0 0 0 2 0 0 1 0 0 0 0 1 0 2 0 0 0 1 0 0 0 0 0 0 0
sentence 3: 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 3 0 0 1 0 0 0 0 0
sentence 4: 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0
sentence 5: 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0
sentence 6: 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0
sentence 7: 0 1 1 0 0 0 0 0 0 0 0 2 0 0 1 0 1 0 0 1 0 2 0 0 0 0 1 0 0 0 0 0 0
sentence 8: 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1
sentence 9: 0 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
sentence 10: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
sentence 11: 0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0
sentence 12: 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 3 0 0 0 0 0
sentence 13: 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
sentence 14: 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 0 0
sentence 15: 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
sentence 16: 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 1 1 0 0 0 0 1 0 0 0 0 0 0
sentence 17: 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
sentence 18: 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
sentence 19: 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 2 0 1 0 1 0 1 0 0 0 0 0 0 0
sentence 20: 0 0 0 0 1 1 0 0 0 2 0 0 1 1 0 1 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 1
實際上它甚至可能是更好的具有相同的列寬,即相同的字符數,使「applianc 「和」向上「具有相同的寬度 – bahamout