[jdom-interest] BETA8 bug
Alex Rosen
arosen at silverstream.com
Thu Mar 21 15:16:17 PST 2002
This new behavior is exactly how it's supposed to work. The List returned
from getChildren() is defined in the API to be a "live" list, but before now
it wasn't as live, so changes to the Element weren't reflected in the
previously-returned list. Now they are.
Your workarounds are fine, though in your second example it would be a
little clearer to use:
while(masterChildren.size() > 0)
and just used get(0) instead of get(j).
Another simple (but perhaps not so obvious) fix is to add something like:
masterList = new ArrayList(masterList);
Since the ArrayList won't change when the parent Element changes, this will
work fine. Another solution is:
Iterator iter = masterList.iterator();
while(iter.hasNext())
{
Element resource = (Element)iter.next();
iter.remove();
entity.addContent(resource);
}
The remove() call will both detach the child from the parent Element, and
also will make sure that the Iterator doesn't get messed up by the removal.
Alex
> -----Original Message-----
> From: jdom-interest-admin at jdom.org
> [mailto:jdom-interest-admin at jdom.org]On Behalf Of Mike Viens
> Sent: Thursday, March 21, 2002 12:06 AM
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] BETA8 bug
>
>
> // masterChildren is populated with elements from a different Document
>
>
> // BETA8 bug?
> // example: when masterChildren has 4 elements, only two are ever
> processed
> // the list (masterChildren) is somehow being modified within the for
> loop
> // when the resource.detach() method is called - this works in BETA7
> for (int j = 0; j < masterChildren.size(); j++)
> {
> Element resource = (Element)masterChildren.get(j);
> // this line causes items in the list to be removed!
> entity.addContent(resource.detach());
> }
>
>
> // solution 1 - to fix it in beta 8
> Element entity = ........
> Vector newChildren = new Vector();
> for (int j = 0; j < masterChildren.size(); j++)
> {
> Element resource = (Element)masterChildren.get(j);
> newChildren.add(resource);
> }
>
> for (int j = 0; j < newChildren.size(); j++)
> {
> Element resource = (Element)newChildren.get(j);
> entity.addContent(resource.detach());
> System.out.println("finished child: " + j + " - total: " +
> newChildren.size());
> }
>
>
> // solution 2 - this also works
> // change this line:
> // for (int j = 0; j < masterChildren.size(); j++)
> // to this:
> // for (int j = 0; j < masterChildren.size(); )
>
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/yo
uraddr at yourhost.com
More information about the jdom-interest
mailing list