【发布时间】:2017-07-27 15:41:15
【问题描述】:
我在从 JTable 获取数据时遇到问题。我需要将其保存到ArrayList<String[]>。我做了一个行和列的循环,但有些东西不起作用,它只保存最后一行......
这是函数示例:
private void saveTable(){
ArrayList<String[]> tableSaved = new ArrayList<>();
String[] rowSaved = new String[headerTable.length];
String cellValue;
for(int row=0;row<table.getModel().getRowCount();row++){
for (int column=0; column<table.getModel().getColumnCount();column++){
cellValue = (String) table.getModel().getValueAt(row, column);
rowSaved[column] = cellValue;
}
tableSaved.add(rowSaved);
// I check here if the output is correct, and It is.
System.out.println("GET");
for(String s:rowSaved){
System.out.print(s+" ");
}
}
//When I check if the tableSaved is correct, It doesn't.
System.out.println("");
System.out.println("ACTUALLY SAVED");
for (String[] row:tableSaved){
for (String s:){
System.out.print(" "+s);
}
System.out.println("");
}
我也试过这个循环
int row=0;
while (row<table.getModel().getRowCount()){
int column=0;
while (column<table.getModel().getColumnCount()){
cellValue = (String) table.getModel().getValueAt(row, column);
rowSaved[column] = cellValue;
column++;
}
tableSaved.add(rowSaved);
row++;
}
它返回最后一行的次数与表中的行一样多...我一直在寻找答案,但我没有解决这个错误
这是一个示例截图
【问题讨论】:
-
我认为你需要添加 tableSaved.add(rowSaved);这在一个循环中。
-
但它在第一个循环内...
标签: java swing arraylist jtable