将数据库查询的值存入ResultSet在放入Arraylist中,怎么获取list中的值,并计算其总和。
ResultSet rs=DbManager.getInstance().query(sql);
List list=new ArrayList();
try {
while(rs.next()){
try {
list.add(rs.getInt(1));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------解决方案--------------------------------------------------------
int sum =0;
for(Iterator<A> it = list.iterator(); it.hasNext(); ) {
sum = sum + (int)it.next()
}
sum 就是你要求的总和了