Values & References16 / 16
Up next

Shallow copy shares nesting

import java.util.ArrayList;
import java.util.List;

class Main {
    public static void main(String[] args) {
        var grid = new ArrayList<List<Integer>>();
        grid.add(new ArrayList<>(List.of(1, 2)));
        // a shallow copy still points at the same inner lists
        var shallow = new ArrayList<>(grid);
        shallow.get(0).add(99);
        System.out.println(grid.get(0));
    }
}
What you're solving

inner objects are still shared