我正在尝试填充一组双精度元素,这些元素存储在一个JTable列中,我尝试使用"for“循环,但算法并不相关:
for (int i = 0 ; i < nRow ; i++)
{
double ad [ ] = { (double)dtm.getValueAt(i, 1)};
}问题是,当"i“变量等于一个新值(例如1或2)时,最后一个值会被新值替换。
什么算法允许填充集合?
发布于 2015-05-03 10:40:34
感谢@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;
}嗨,检查上面的内容可能会对你有所帮助。
发布于 2015-05-03 19:25:20
您需要为数组的每个元素分配一个单独的值,例如...
TableModel dtm = ...;
int nRow = dtm.getRowCount();
double ad[] = new double[nRow];
for (int i = 0 ; i < nRow ; i++)
{
ad[i] = (double)dtm.getValueAt(i, 1);
}有关详细信息,请参阅Arrays
https://stackoverflow.com/questions/30009601
复制相似问题