ArrayList<Memory> memories = new ArrayList<>();
        memories.add(new Memory());
        memories.add(new Memory());
        memories.add(new Memory());

        ArrayList<Process> processes = new ArrayList<>();
        Process p1 = new Process("p1",0,800,2, Color.BLUE); processes.add(p1);
        Process p2 = new Process("p2",0,200,2, Color.YELLOW); processes.add(p2);
        Process p3 = new Process("p3",0,200,3, Color.RED); processes.add(p3);

        for (int i = 0; i < memories.size(); i++) {

            memories.set(i, i != 0 ? memories.get(i-1) : new Memory());
            Memory m = memories.get(i).bestSlot(processes.get(i));
            memories.set(i,m);
        }

        for(int j = 0; j < memories.size(); j++){
            System.out.println(memories.get(j));
        }

I'm trying to update an element of an array every iteration but it keeps setting the same value for every element behind.

It gives me this output:

    [ ( 0: 800 | Process: p1)  ( 1: 200 | Process: p2)  ( 2: 200 | Process: p3)  ( 3: 800 ) ]
    [ ( 0: 800 | Process: p1)  ( 1: 200 | Process: p2)  ( 2: 200 | Process: p3)  ( 3: 800 ) ]
    [ ( 0: 800 | Process: p1)  ( 1: 200 | Process: p2)  ( 2: 200 | Process: p3)  ( 3: 800 ) ]

While it should be something like this:

    [ ( 0: 800 | Process: p1)  ( 1: 1200 ) ] 
    [ ( 0: 800 | Process: p1)  ( 1: 200 | Process: p2)  ( 2: 1000 ) ] 
    [ ( 0: 800 | Process: p1)  ( 1: 200 | Process: p2)  ( 2: 200 | Process: p3)  ( 3: 800 ) ]

The changes start while updating the content of memory of each iteration (in this case with bestSlot but it shouldn't have access to the other elements itself).


Here's the code: [repository][1]


  [1]: https://github.com/ear64-ua/SO