0
LinkedList<CasosPossiveis> casos = new LinkedList<>();
CasosPossiveis objeto = new CasosPossiveis();

for (int i=0; i<10; i) {
    //modifying objeto values here
    casos.add(objeto);
}

Everytime I modify objeto values, every position at casos is also being modifyied. I belive that casos.add(objeto) is using reference of objeto. What I need is everytime I add a new object to casos and modify objeto, it wont modify the value already added to the list.

Exemple:

objeto.setId=1;
casos.add(objeto);
objeto.setId=2;
casos.add(objeto);

What I expect when i print casos:

1, 2

What my code is priting:

2, 2
Mureinik
  • 297,002
  • 52
  • 306
  • 350
Marcos
  • 1

4 Answers4

4

You are adding the same object in every casos , try this :

  LinkedList<CasosPossiveis> casos = new LinkedList<>();

     for (int i=0; i<10; i) {
     CasosPossiveis   objeto = new CasosPossiveis();
     //modifying objeto values here
      casos.add(objeto);
 }
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
2

You have a single object, objecto, which you add multiple times to the list. If you want the list to contain different instances, you'll have to create new ones:

for (int i=0; i<10; i) {
    // Use a new instance in every iteration
    CasosPossiveis objeto = new CasosPossiveis();

    //modifying objeto values here

    casos.add(objeto);
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Small error. You are creating object of CasosPossiveis only one time. When java runs this code, it creates object before for loop.

Now when for loop is processed then it is using that same object for 10 times. Hence same value. So for each time to get a new object you need to create object each time loop runs.

For that to happen- Write CasosPossiveis objeto = new CasosPossiveis(); Inside for loop.

Neeraj Yadav
  • 422
  • 6
  • 13
0

You add same instance of CassosPositives class in list, you have multiply links to same object in every position in list, instead you need to create new instance of your class every time you place it in list:

Class CassosPositives should be like:

public class CassosPositives {
    private int id;

    CassosPositives(int id) {
        this.id = id;
    }

    public int getId() { 
        return id; 
    }

    public void setId(int id) { 
        this.id = id; 
    }

}   

Example (java 8 way)

        // create list of CassosPositives
        List<CassosPositives> casos = IntStream.iterate(0, i -> i + 1)
                                                .limit(10)
                                                .map(i -> new CassosPositives(i))
                                                .collect(Collectors.toList());
        //print list of CassosPositives
        casos.forEach(caso -> System.out.println(caso.getId()));    

Example (java 7 way)

    // create list of CassosPositives
    List<CassosPositives> casos = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        casos.add(new CassosPositives(i));
    }

    //print list of CassosPositives
    for(CassosPositives caso : casos) {
        System.out.println(caso.getId());
    }
fxrbfg
  • 1,756
  • 1
  • 11
  • 17