Why Does My Arraylist Contain N Copies of the Last Item Added to the List

Why does my ArrayList contain N copies of the last item added to the list?

This problem has two typical causes:

  • Static fields used by the objects you stored in the list

  • Accidentally adding the same object to the list

Static Fields

If the objects in your list store data in static fields, each object in your list will appear to be the same because they hold the same values. Consider the class below:

public class Foo {
private static int value;
// ^^^^^^------------ - Here's the problem!

public Foo(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}

In that example, there is only one int value which is shared between all instances of Foo because it is declared static. (See "Understanding Class Members" tutorial.)

If you add multiple Foo objects to a list using the code below, each instance will return 3 from a call to getValue():

for (int i = 0; i < 4; i++) {      
list.add(new Foo(i));
}

The solution is simple - don't use the static keywords for fields in your class unless you actually want the values shared between every instance of that class.

Adding the Same Object

If you add a temporary variable to a list, you must create a new instance of the object you are adding, each time you loop. Consider the following erroneous code snippet:

List<Foo> list = new ArrayList<Foo>();    
Foo tmp = new Foo();

for (int i = 0; i < 3; i++) {
tmp.setValue(i);
list.add(tmp);
}

Here, the tmp object was constructed outside the loop. As a result, the same object instance is being added to the list three times. The instance will hold the value 2, because that was the value passed during the last call to setValue().

To fix this, just move the object construction inside the loop:

List<Foo> list = new ArrayList<Foo>();        

for (int i = 0; i < 3; i++) {
Foo tmp = new Foo(); // <-- fresh instance!
tmp.setValue(i);
list.add(tmp);
}

ArrayList of POJO contains only the last item added.

You need to create the instance of E per iteration, but you created only one instance of E.

My ArrayList only contains the last item on my array

The problem is that your fields in the Student class are static. You haven't shown the code, but it could be guessed as this:

public class Student {

private static int id;
//other fields...
//constructor...
//getters and setters...
}

Just remove the static marker from the fields in this class.

public class Student {

//field must not be static
private int id;

//other non-static fields...
//constructor...
//getters and setters...
}

Note that removing the static marker in the fields of Converter class won't fix the problem.

In short, static fields belong to the class, while non-static fields belong to the class object reference. If having static fields in your Student class, all the object references will share this value. When having non-static fields, since they belong to each instance of the class, every object reference will have a different value.

More info: Understanding Instance and Class Members

Array List showing only most recent object added

Your program should be like this. Everytime you are setting data in fuOb it is overriding the previous data..

FultonObj fuOb1 = new FultonObj(); 
fuOb1.setFileNo(file1);
fuOb1.setParcelId(parcelId1);
fuOb1.setSitus(situs1);
list.add(fuOb1);

FultonObj fuOb2 = new FultonObj();
fuOb2.setFileNo(file2);
fuOb2.setParcelId(parcelId2);
fuOb2.setSitus(situs2);
list.add(fuOb2);

ArrayList returns last item for all entries

You need to make Gender and Age non-static otherwise these fields will only retain a single value per class member variable:

public String gender;
public Integer age;

Aside: Java naming conventions indicate that variables start with lowercase letters making Gender gender, etc.

ArrayList added item is replaced by the next item in for loop

You have a single reference. By setting the objOutwardMessage2to objOutwardMessageyou are just changing the data inside the reference.



Related Topics



Leave a reply



Submit