Correct way to add objects to an ArrayList
Posted
by
ninjasense
on Stack Overflow
See other posts from Stack Overflow
or by ninjasense
Published on 2010-08-19T23:40:11Z
Indexed on
2011/02/26
7:25 UTC
Read the original article
Hit count: 217
I am trying to add an object to an arraylist but when I view the results of the array list, it keeps adding the same object over and over to the arraylist. I was wondering what the correct way to implement this would be.
public static ArrayList<Person> parsePeople(String responseData) {
ArrayList<Person> People = new ArrayList<Person>();
try {
JSONArray jsonPeople = new JSONArray(responseData);
if (!jsonPeople.isNull(0)) {
for (int i = 0; i < jsonPeople.length(); i++) {
Person.add(new Person(jsonPeople.getJSONObject(i)));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
}
return People;
}
I have double checked my JSONArray data and made sure they are not duplicates. It seems to keep adding the first object over and over.
© Stack Overflow or respective owner