aspose file tools
The moose likes Java in General and the fly likes Java taking reference of LinkedList for updations Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Java taking reference of LinkedList for updations" Watch "Java taking reference of LinkedList for updations" New topic
Author

Java taking reference of LinkedList for updations

Last Samurai
Greenhorn

Joined: Aug 09, 2011
Posts: 2

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

Joined: Mar 09, 2010
Posts: 256
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

Joined: Nov 04, 2008
Posts: 146

Hi,

Please see code and comments.


Thanks


The important thing is not to stop questioning.Curiosity has its own reason for existing.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19232

The creation of list3 can be shorter:
Other than that, your code and comments are spot-on.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Last Samurai
Greenhorn

Joined: Aug 09, 2011
Posts: 2

Thanks a lot for your response. I've implemented it but yet to test!! Will get back in case of any issues.

Thanks again
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Java taking reference of LinkedList for updations
 
Similar Threads
LinkedList problem
benefits of using base class/interface reference to refer sub class objects.
Instance in derived class
Using Lists
How to Convert one type of Collection object data's to Another Collection Object