如何填充对象矩阵

How can I fill an object matrix

我有一个对象矩阵,我必须用字符串填充它。我怎样才能做到这一点?让我解释一下,我已经声明了一个固定大小的对象矩阵,现在我必须通过 for 循环填充它,我如何访问矩阵的单元格。我把矩阵的代码放了下来。抱歉,是意大利语。乔鲁诺 = 天,矿石 = 小时

tabMedie = new JTable();
tabMedie.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
tabMedie.setFont(new Font("Times New Roman", Font.PLAIN, 10));
tabMedie.setModel(new DefaultTableModel(
    new Object[][] {
        {"Orari", "Giorno 1", "Giorno 2", "Giorno 3", "Giorno 4", "Giorno 5", "Giorno 6", "Giorno 7"},
        {"1", "", "", "", "", "", "", ""},
        {"2", "", "", "", "", "", "", ""},
        {"3", "", "", "", "", "", "", ""},
        {"4", "", "", "", "", "", "", ""},
        {"5", "", "", "", "", "", "", ""},
        {"6", "", "", "", "", "", "", ""},
        {"7", "", "", "", "", "", "", ""},
        {"8", "", "", "", "", "", "", ""},
        {"9", "", "", "", "", "", "", ""},
        {"10", "", "", "", "", "", "", ""},
        {"11", "", "", "", "", "", "", ""},
        {"12", "", "", "", "", "", "", ""},
        {"13", "", "", "", "", "", "", ""},
        {"14", "", "", "", "", "", "", ""},
        {"15", "", "", "", "", "", "", ""},
        {"16", "", "", "", "", "", "", ""},
        {"17", "", "", "", "", "", "", ""},
        {"18", "", "", "", "", "", "", ""},
        {"19", "", "", "", "", "", "", ""},
        {"20", "", "", "", "", "", "", ""},
        {"21", "", "", "", "", "", "", ""},
        {"22", "", "", "", "", "", "", ""},
        {"23", "", "", "", "", "", "", ""},
        {"24", "", "", "", "", "", "", ""},
    },
    new String[] {
        "Ore", "Giorno 1", "Giorno 2", "Giorno 3", "Giorno 4", "Giorno 5", "Giorno 6", "Giorno 7"
    }
) {
    Class[] columnTypes = new Class[] {
        String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class
    };
    public Class getColumnClass(int columnIndex) {
        return columnTypes[columnIndex];
    }
    boolean[] columnEditables = new boolean[] {
        true, false, false, false, true, true, true, true
    };
    public boolean isCellEditable(int row, int column) {
        return columnEditables[column];
    }
});
tabMedie.getColumnModel().getColumn(0).setResizable(false);
tabMedie.getColumnModel().getColumn(1).setResizable(false);
tabMedie.getColumnModel().getColumn(2).setResizable(false);
tabMedie.getColumnModel().getColumn(3).setResizable(false);
tabMedie.getColumnModel().getColumn(4).setResizable(false);
tabMedie.getColumnModel().getColumn(5).setResizable(false);
tabMedie.getColumnModel().getColumn(6).setResizable(false);
tabMedie.getColumnModel().getColumn(7).setResizable(false);
tabMedie.setBounds(503, 36, 662, 401);
frmTemperature.getContentPane().add(tabMedie);

首先,将“矩阵”(实际上是一个two-dimensional数组)存储在一个变量中:

String[][] texts = { { "Ora", "Giorno 1", … }, … };

然后,您可以在 for 循环中使用它:

for (int x = 1; x < 8; x++) {
    for (int y = 1; y < texts.length; y++) {
        texts[y][x] = "Your Text Here";
    }
}

最后,您可以将它作为参数传递给方法:

new DefaultTableModel(texts, new String[] { … }) { … };