serv-inc
serv-inc

Reputation: 38247

Two level deep copy in Java

In Java, it's easy to deeply clone an object with Object.clone() and also to shallow-clone (let's call it level 1).

How do you go about doing a two-level deep copy, that is, copying all primitives, but also creating (shallow) copies of all Object members ?

Let's say you copy a phone book with a list of number entries . A shallow copy would just

clone = original.getLNumberlist();

a deeper copy would do a

clone = new ArrayList<>(original.getNumberlist());

there are many questions about deep and shallow cloning, but I could not find any Java question about this two-level cloning, let alone its implementation

Upvotes: 0

Views: 250

Answers (0)

Related Questions