java 在 while 循环中将对象添加到列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25940999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding and object to a list inside a while loop
提问by stackUser2000
I'm trying to loop a list and that list have 4 arrays inside and each array have 7 values
我正在尝试循环一个列表,该列表中有 4 个数组,每个数组有 7 个值
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).
我正在循环列表,然后当我从列表中获取一个数组时,因为我知道该数组有多少个值,所以我像这样分配每个数组索引:personObject.setName(String.valueOf(myArray[0])然后在 while 循环结束时我添加到 Persons 列表中像这样的personObject: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:
我的问题是我的列表只填充了相同的对象,我知道我循环的列表中的 4 个数组具有不同的值,这是我的代码:
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.
当我打印我的人员列表时,里面全是同名的人,姓氏和昵称,是一个充满具有相同值的相同对象的列表。
采纳答案by nem035
Each time you are adding a Personyou want to add a differentperson right?
每次添加一个时,Person您都想添加不同的人,对吗?
Therefore, you should create a newPersonobject inside your loop every time.
Not create only one outside of the loop whose data you just keep changing.
因此,您应该每次都在循环内创建一个对象。不要只在循环之外创建一个您不断更改其数据的数据。newPerson
// 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
}
回答by Elliott Frisch
You are adding (and modifying) the same object instance to the loop multiple times because you only create one Personinstance, move that into the loop.
您多次向循环添加(和修改)同一个对象实例,因为您只创建了一个Person实例,并将其移到循环中。
// 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.
您将创建多个实例并将它们添加到您的循环中。
回答by Rudra
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]andPerson p = new Person()inside loop to set new value otherwise it will get override.
在查看您的代码后,它看起来像我。
您不是从相关数组中读取数据,即innerObj. 替换为myArray[0],它应该是innerObj[0]和Person p = new Person()内部循环来设置新值,否则它将被覆盖。
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);
}
回答by Olli Puljula
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);
}
}
}

