I want to make a deep copy of a generic list in Java. How could I improve this code?
public interface Deep{}
public class Class1() implements Deep{
public Class1(Class1 o){
//implementation of deep copy
}
}
public class Class2() implements Deep{
public Class2(Class2 o){
//implementation of deep copy
}
}
Implementing deepCopy of List in another class:
public List<Deep> deepCopy(List<Deep> inList){
if(inList.get(0) instanceof Class1){
List<Deep> newList=new ArrayList<Class1>();
for (Deep deep : inList)
newList.add(new Class1((Class1) deep));
}
else if(inList.get(0) instanceof Class2){
List<Deep> newList=new ArrayList<Class2>();
for (Deep deep : inList)
newList.add(new Class2((Class2) deep));
}
}
And here is the call:
List<Class1> list= new List<Class1>();
//fill list...
List<Class1> aCopy=(List<Class1>) deepCopy(list);