这个Iierator错在哪?请大家看看
很简单的几行代码:
while(rs.next()){
person.setId(rs.getInt(1));
person.setName(rs.getString(2));
person.setPassword(rs.getString(3));
person.setAge(rs.getInt(4));
System.out.print(person.getId()); //输出(1)
System.out.print(person.getName());
System.out.print(person.getPassword());
System.out.print(person.getAge());
list.add(person);
}
Iterator it=list.iterator();
while(it.hasNext()){
Person p=(Person) it.next();
System.out.print(p.getId());
System.out.print(p.getName());
System.out.print(p.getPassword());
System.out.print(p.getAge());
}
return list;
输出(1)
1 a a 1
2 b b 2
3 c c 3
输出(2)
用完迭代器后为什么输出的是
3 c c 3
3 c c 3
3 c c 3
不知道错在哪,请高手看看,指点一下
------解决方案--------------------
Java code
while(rs.next()){
//person 放到这里实例化
person=new Person();
person.setId(rs.getInt(1));