java.util.List
does not implement Cloneable
, so the compiler cannot ensure that the concrete implementation you are using does. So, it won't allow you to write a code that calls a method that may not be there.
If you know the concrete implementation, you can cast it. V.g., if it is an ArrayList<List<Test>>
(since ArrayList
does implement Cloneable
), then you do
b = (List<List<Test>>) ((ArrayList<List<Test>>) a).clone();
If you do not, implement your own method.
Remember that the default clone
makes shallow copies. That is, the object returned will be a new List<List<Test>>
, but the inner List<Test>
and Test
will not get copied and you will get references to the original objects.