[jdom-interest] slightly off topic java.util.List and getChild problem

Aron Kramlik aron.kramlik at itouch.com.au
Wed Oct 3 20:02:07 PDT 2001


I was wondering why the following code would produce double instances
of the "system-message" elements in the object, processMessagesList.
I have an Element that could contain zero or more sub-elements with
a name of "user-message" and/or "system-message".  What I want to do
is get a List of both of them into one list so that I can pass that
list onto something else that will log them.
..
List processMessagesList =
serviceResponse.getResponse().getChildren("user-message");
processMessagesList.addAll(serviceResponse.getResponse().getChildren("system-message"));
..
Here the processMessagesList will have two elements for each
"system-message"
element.  I have fixed the problem by replacing the code with:
..
List userMessagesList =
serviceResponse.getResponse().getChildren("user-message");
List systemMessagesList =
serviceResponse.getResponse().getChildren("system-message");
List processMessagesList = new java.util.ArrayList(userMessagesList);
processMessagesList.addAll(systemMessagesList);
..
This way it works.

Why is this?  Maybe I do not understand the List interface
well enough so could someone that does explain this please to me.




More information about the jdom-interest mailing list