I’m trying to create a generic container which has a copy-constructor. I’m having trouble using the clone method even though I have coded it in. Here’s what I have so far:
public class MyBox<T>
{
private List<T> list;
public MyBox()
{
list = new ArrayList<T>();
}
public void add(T item )
{
list.add(item);
}
public MyBox(MyBox<T> other) throws CloneNotSupportedException //this is giving me trouble
{
for(T item : other.list)
{
list.add((T) item.clone());
}
}
}
How can I get my copy-constructor to work?
Usually you don’t need to clone the item.
When you add an item from collection A to collection B, the item is now referenced by both two collection A and B.