[英]Why do I get the same value from different objects an an arrayList?
I add objects to my arraylist from a database table like this: 我将对象从数据库表添加到arraylist中,如下所示:
private void fillArray() {
for (People temp : peopleList) {
nameT = temp.getFirstName();
IDT = temp.getUserid();
users.add((new User(IDT, nameT) {
@Override
public String toString() {
return ID + "," + name;
}
}));
}
}
Wanting to display the elements of the objects in the arraList I separate them by overriding toString() and then using Scanner with the delimiter ",". 想要显示arraList中对象的元素,我通过覆盖toString(),然后使用带有定界符“,”的Scanner来分离它们。
private void fieldSplitter(String object) {
Scanner inline = new Scanner(object).useDelimiter(",");
IDT = inline.next();
nameT = inline.next();
JOptionPane.showMessageDialog(null, IDT + " " + nameT, "Success", JOptionPane.INFORMATION_MESSAGE);
IDT = null;
nameT = null;
}
This should display all the different elements of each object. 这应该显示每个对象的所有不同元素。 All I get is the last entry in my database table displayed every time even though there are 4 other entries.
我得到的只是每次显示的数据库表中的最后一个条目,即使还有其他4个条目也是如此。 What have I done wrong?
我做错了什么?
You should use the getters of your class User to have something cleaner : 您应该使用类User的getter获得更干净的东西:
users.add((new User(IDT, nameT) {
@Override
public String toString() {
return getId() + "," + getName();
}
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.