• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java taking reference of LinkedList for updations  RSS feed

 
Last Samurai
Greenhorn
Posts: 2
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm populating a LinkedList of Strings from database. Also Im assigning this list into different objects. when i try to add a new element in the linked list from a particular object, it updates all the objects in which i assigned the list into. Probably it is taking the reference of the list in the objects.

How can i solve this problem. Is there any other way to do it?

Any help is very appreciated.
Thanks in advance
 
bhanu chowdary
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically you are assigning the same Linkedlist to different variables. Since they all refer to the same object the change will be reflected everywhere.
Use a different linkedlist for the one you are adding.
 
Vijay Tidake
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Please see code and comments.


Thanks
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The creation of list3 can be shorter:
Other than that, your code and comments are spot-on.
 
Last Samurai
Greenhorn
Posts: 2
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your response. I've implemented it but yet to test!! Will get back in case of any issues.

Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!