Adding and object to a list inside a while loop

徘徊边缘 提交于 2021-02-04 16:11:26

问题


I'm trying to loop a list and that list have 4 arrays inside and each array have 7 values

I am looping the list and then when I get an array from the list, since I know how many values have the array I assign each array index like this: personObject.setName(String.valueOf(myArray[0]) and then at the end of the while loop I add to a list of Persons the personObject like this: listOfPersons.add(personObject).

My problem is that my list only gets populate with the same object and I know that the 4 arrays inside the list that I'm looping have different values, here is my code:

ArrayList<Object> objectList= null;
objectList= _serviceVariable.getObjects(); <--- this works it returns a list with 4 arrays all with diferent values

Person myPerson = new Person();
List<Person> personsList = new List<Person>; 

Iterator<Object> itr = objectList.iterator();

while(itr.hasNext())
        {
Object[] innerObj = (Object[]) itr.next();

myPerson.setName(String.valueOf(innerObj [0])
myPerson.setLastName(String.valueOf(innerObj [1])
myPerson.setNickName(String.valueOf(innerObj [2])

personsList.add(myPerson); 
}

when I print my persons list is full of persons with the same name, lastName and nick name, is a list full of the same objects with the same values.


回答1:


Each time you are adding a Person you want to add a different person right?

Therefore, you should create a new Person object inside your loop every time. Not create only one outside of the loop whose data you just keep changing.

// NOT HERE Person myPerson = new Person();
// ...

while(itr.hasNext()) {
    Object[] innerObj = (Object[]) itr.next();

    Person myPerson = new Person(); // HERE you create a new Person        
    myPerson.setName(String.valueOf(myArray[0]);
    myPerson.setLastName(String.valueOf(myArray[1]);
    myPerson.setNickName(String.valueOf(myArray[2]);

    personsList.add(myPerson); // add the new person to the list
}



回答2:


You are adding (and modifying) the same object instance to the loop multiple times because you only create one Person instance, move that into the loop.

// Person myPerson = new Person();
// ...
while(itr.hasNext()) {
  Person myPerson = new Person(); // <-- HERE!
  Object[] innerObj = (Object[]) itr.next();
  myPerson.setName(String.valueOf(myArray[0])
  myPerson.setLastName(String.valueOf(myArray[1])
  myPerson.setNickName(String.valueOf(myArray[2])
  personsList.add(myPerson); 
}

And you will create multiple instances and add them to your loop.




回答3:


After reviewing your code, it looks to me .
You are not reading data from related array, which is innerObj. replace of myArray[0], it should be innerObj[0] and
Person p = new Person() inside loop to set new value otherwise it will get override.

while(itr.hasNext()) {
  Person p= new Person(); // <-- HERE!
  Object[] innerObj = (Object[]) itr.next();
  p.setName(String.valueOf(innerObj [0])
  p.setLastName(String.valueOf(innerObj [1])
  p.setNickName(String.valueOf(innerObj [2])
  personsList.add(p); 
}



回答4:


public class Foobar {
    class Person {
        String name;

        Person(String name) {
            this.name = name;
        }
    }

    class SpecialPerson extends Person {
        SpecialPerson(String name) {
            super(name);
        }
    }

    public void myFoobar() {
        List<List<Person>> ListOfPersonsList = new ArrayList<List<Person>>();

        List<Person> persons1 = new ArrayList<Person>();
        persons1.add(new Person("Foo"));

        List<Person> persons2 = new ArrayList<Person>();
        persons2.add(new Person("Bar"));

        ListOfPersonsList.add(persons1);
        ListOfPersonsList.add(persons2);

        List<SpecialPerson> specials = new ArrayList<SpecialPerson>();

        for (List<Person> persons : objectList) {
            for (Person person : persons) {
                specials.add(new SpecialPerson(person.name));
            }

        }

        for (SpecialPerson special : specials) {
            System.out.println("Name: " + special.name);
        }

    }

}


来源:https://stackoverflow.com/questions/25940999/adding-and-object-to-a-list-inside-a-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!