CustomerBean cb = new CustomerBean();
while(rs.next()){
cb.setUserId( rs.getInt("UserId"));
cb.setUserName( rs.getString("UserName"));
cb.setPassword( rs.getString("Password"));
cb.setTel( rs.getString("Tel"));
cb.setEmail( rs.getString("Email"));
cb.setPriority( rs.getInt("Priority"));
al.add(cb);
}
这个时候,al(ArrayList)插入都是同一条记录。
如果把
CustomerBean cb = new CustomerBean();
放在while循环中,则正确插入。
这个问题发现,集合中存放的是对象引用,一旦引用发生改变,所有对象都将发生变化。