I am trying to write a procedure do the deep copy of List<List<Integer>>, and I am doing like this:
public static List<List<Integer>> clone(final List<List<Integer>> src) { List<List<Integer>> dest = new ArrayList<List<Integer>>(); for( List<Integer> sublist : src) { List<Integer> temp = new ArrayList<Integer>(); for(Integer val: sublist) { temp.add(val); } dest.add(temp); } return dest ; } Is this a good way to do? Is it possible to get rid of the inner loop? The fact is that each of the inner sub-lists can grow to large lengths.
Integeris immutable; anything you do to anIntegeris lost unless you re-add it back into the list in its exact place. Why do you think you want a deep copy?memcpyonly because in C it's generally faster than manually iterating and copying, not becausememcpyhas any semantic value. He/she is essentially asking "am I doing a deep copy as efficiently as possible?".memcpy. See my updated answer.