• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

addAll method in List Strange Behaviuor

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome to javaranch Anudeep!
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic