我想获取数组中的值。稍后我想在另一个类中使用该数组。如何才能做到这一点?
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 1) {
JTable target = (JTable) e.getSource();
int row = target.getSelectedRow();
int column = target.getSelectedColumn();
row = table.convertRowIndexToModel(row);
String val1 = (String) table.getModel().getValueAt(row, 0);
String val2 = (String) table.getModel().getValueAt(row, 1);
String val3 = (String) table.getModel().getValueAt(row, 2);
String val4 = (String) table.getModel().getValueAt(row, 3);
String val5 = (String) table.getModel().getValueAt(row, 4);
System.out.println(val1 + " " + val2 + " " + val3 + " " + val4 + " " + val5);
}
}
});
最佳答案
用您要添加的值的大小实例化一个新数组。如果您不知道尺寸,您应该使用 ArrayList 来查看
String[] arr = new String[5];
将值添加到数组中。您可以将其放入循环中,这样代码就会更简洁。
String val1 = (String) table.getModel().getValueAt(row, 0);
arr[0] = val1;
String val2 = (String) table.getModel().getValueAt(row, 1);
arr[1] = val2;
...
然后返回数组
return arr;
关于java - 字符串作为 ArrayList 并在另一个类中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42912089/