Hi,
I'm trying to write a function that takes a collection of generic types and copies (i.e. clones) each element of the collection into a new collection. The problem is, clone() is protected and the Cloneable interface doesn't even make it public... I was thinking of using something like this, but of course the compiler doesn't like the call to clone().
How can I create a generic function that only accepts collections where T.clone() is public?
Also, what's the point of Cloneable? If it doesn't define clone() as public, what's the point of implementing it?
I'm trying to write a function that takes a collection of generic types and copies (i.e. clones) each element of the collection into a new collection. The problem is, clone() is protected and the Cloneable interface doesn't even make it public... I was thinking of using something like this, but of course the compiler doesn't like the call to clone().
Code:
public static <T extends Cloneable>
void Copy( Collection<T> lhs, Collection<T> rhs )
{
lhs.clear();
Iterator<T> it = rhs.iterator();
while ( it.hasNext() == true )
{
lhs.add( it.next().[b]clone()[/b] );
}
}
Also, what's the point of Cloneable? If it doesn't define clone() as public, what's the point of implementing it?