GeeCON Prague 2014*
The moose likes Java in General and the fly likes addAll method in List Strange Behaviuor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "addAll method in List Strange Behaviuor" Watch "addAll method in List Strange Behaviuor" New topic
Author

addAll method in List Strange Behaviuor

Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29


why the last system.out.println(rawlist) is printing RawList rawList after addAll [a, 10, [b, c, a, 10, (this Collection), 20, d], 20] instead of a, 10, [b, c], 20
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You first add stringList to rawList. You then add everything that is in rawList to stringList. That includes stringList itself. In other words, stringList.contains(stringList). At this moment, stringList is no longer [b, c] but [b, c, a, 10, <stringList itself>, 20].
If you print out stringList, it will call toString() on stringList. This in turn calls toString() on all of the elements. One of these elements is stringList itself. The (this Collection) part is there to prevent toString() to be called again, and again, and again, until you get a StackOverflowError.

Remember, in Java, variables are references (or primitives). When you add stringList to rawList you don't add its contents but the reference to the very same object. To get the result you want, you must add a copy of stringList to rawList. There are two ways:
1) rawList.add(stringList.clone()). This will only work if stringList is declared as a LinkedList, ArrayList or any other type that has a public clone() method. If you would declare both as List instead this would not work.
2) rawList.add(new ArrayList(stringList)) or rawList.add(new LinkedList(stringList)). This will create a new list with the same contents as stringList, and this new list is added to rawList.


Note that your code has a serious flaw. You suppress the warnings, but in this case that's unjustified. I can make your code throw an exception by adding just one line:
That's because your LinkedList<String> not only contains Strings, but also Integers and a LinkedList.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

welcome to javaranch Anudeep!
Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
Thank you for your reply.. but sorry i am unable to understand why the rawlist is again updated.. we are just adding rawList to StringList by stringlist.addAll(rawlist)

please explain...
Thank you
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Like I said, rawList contains a reference to stringList, not its contents. When you add all elements of rawList to stringList, you are again adding references to the elements. Afterwards, stringList contains a reference to itself, as that was what originally in rawList.
 
GeeCON Prague 2014
 
subject: addAll method in List Strange Behaviuor