Adding and object to list inside while loop

I am trying to loop through a list and this list contains 4 arrays inside and each array has 7 values

I loop through the list and then when I get the array from the list, since I know how many values ​​the array has, 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 the list of persons, for personObject: listOfPersons. add (personObject).

My problem is that my list is only filled with the same object, and I know that the 4 arrays inside the list that I loop 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 the list of my faces, populated with people with the same name, lastName and nickname, is a list full of the same objects with the same values.

+3


source to share


4 answers


Every time you add Person

, do you want to add another person?

Therefore, you must create an object inside the loop every time . Don't create just one outside of the loop whose data you just change. new

Person



// 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
}

      

+3


source


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

, move it 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.

+2


source


After looking at the code it seems to me.
You are not reading data from a sibling array that innerObj

. replace myArray[0]

, it must be innerObj [0] and
 Person p = new Person()

inside the loop to set the new value, otherwise it will be canceled.

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); 
}

      

0


source


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);
        }

    }

}

      

0


source







All Articles