使用标签格式化矩阵

时间:2016-11-30 06:57:52

标签: java matrix formatting

好的,这可能是一个非常基本的问题,但我对我的生活无法弄清楚Java输出格式,我只是没有任何练习。我正在构建一个术语文档矩阵,用于计算一系列句子中某些单词的出现次数。使用以下代码

可以很好地完成矩阵
for(int i=0; i<rows; i++){
            for(int j=0; j<columns; j++){
                System.out.print(String.format("%3s", matrix[i][j]));
            }
            System.out.println("");
}

输出:

  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  0  0  0  0  1  0  0  0  0  0  0  0  0
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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

但是我想添加行和列标识符,使它看起来更像这样(列标题来自字符串数组,并且会有20个句子):

             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:

Sentence 2:

Sentence 3:
.
.
.

将行和列排列在一起,以便它们易于识别,可能使用网格或其他东西作为分隔符。我对如何实现这一点毫无头绪。

1 个答案:

答案 0 :(得分:0)

按照你的例子,这样的事情应该起作用

@media screen and (max-width: 680px) {
  ul.topnav li:not(:first-child) {
    display: none;
  }

  ul.topnav li {
    float:right;
  }

  ul.topnav li.icon {
    display: inline-block;
  }
}

输出:

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