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). While creating a new instance of Memory it creates a single slot with 2000 as value and no process assigned

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

-------------------------------------------------------------------

Cada vez que actualizo un elemento, sea eliminar algo o añadir, se refleja en los elementos anteriores también.

La salida es algo como esto:

    [ ( 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 ) ]

Cuando debería dar algo como esto:

    [ ( 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 ) ]

Los cambios empiezan a ocurrir cuando uso el método bestSlot, que no debería poder acceder a los elementos de la lista, entonces dudo que este sea el problema


Aquí está el código entero: [repositorio][1]

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