我正在尝试用JTable列中存储的元素填充一组双重元素,我尝试使用“ for”循环,但是算法不相关:
for (int i = 0 ; i < nRow ; i++)
{
double ad [ ] = { (double)dtm.getValueAt(i, 1)};
}
问题在于,当“ i”变量等于新值(例如1或2)时,最后一个值将被新值替换。
哪种算法可以填充集合?
最佳答案
感谢@camick和@madprogrammer更新了代码,根据需要这可能是最佳的。
public Object[] getColumnData(JTable table, int targetColIndex) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
if(table== null)
return null;
if(targetColIndex > nCol)
return null;
Object[] tableData = new Object[nRow];
for (int i = 0 ; i < nRow ; i++)
tableData[i] = dtm.getValueAt(i,targetColIndex);
return tableData;
}
您好,以上内容可能对您有帮助。