[jdom-interest] Moving Children

Jason Hunter jhunter at collab.net
Fri Jun 15 10:23:13 PDT 2001


Kevin Baynes wrote:
> 
> Please forgive me for being slow. There is no java.util.List.clone() but
> there is a java.util.LinkedList.clone() and ArrayList.clone(). Took me a
> while to find that. :-)

Good point.  That kills my idea.

> So moving children should work like so?:
> 
> // Assume creation of two Elements usersA and usersB, each with children
> java.util.List       listA = usersA.getChildren();
> java.util.List       listB = usersB.getChildren();
> 
> java.util.LinkedList cloneList = new LinkedList(listA); // could use
> ArrayList also
> java.util.LinkedList cloneList = (LinkedList)cloneList.clone(); //shallow
> clone, just pointers
> 
> listA.clear(); // call clear() to detach children, effectively *moving* them
> into cloneList?

If you do the constructor above, then the clear() on the new list won't
change the backing list.  It'd work if you cast it to a LinkedList, but
that makes you dependent on the current List choice which is going to
change.  Too bad Cloneable doesn't have a clone() method.  If it did,
you could cast to Cloneable.

Looks like writing a little utility method may be your most elegant
solution.

-jh-



More information about the jdom-interest mailing list