我正在尝试填充一组双元素,这些元素存储在 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;
}
您好,检查以上内容可能对您有帮助。
关于java - 如何通过 JTable 列元素填充一组元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009601/