简体   繁体   English

为什么我从arrayList的不同对象中获得相同的值?

[英]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.

相关问题 我总是得到对象数组列表的最后一个值 - I always get the last value of an arraylist of objects 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class? 从对象的 ArrayList 中获取具有最低值的属性 - Get property with lowest value from ArrayList of objects 为什么无法从ArrayList中获取值? - Why can't I get a value from an ArrayList? 如何从此arraylist中获取值并显示包含它的文本? - How do I get the value from this arraylist and display the text containing it? 如何从 ArrayList 获取具有相同对象属性的对象列表 - How to get a list of objects with same objects property from an ArrayList by 如何使用ArrayList制作一个表,该表保存继承的类中的不同对象? - How do I make a table with my ArrayList that holds different objects from inherited classes? 从具有两个不同大小/长度的两个arraylist获取相同值的索引 - Get index of the same value from two arraylist with two different sizes/length 如何遍历arrayList将它们存储为不同的对象? - How do I loop through an arrayList to store them as different objects? 如何创建充满不同类型对象的ArrayList? - How do I create an ArrayList filled with different types of objects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM