Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

Strange things with java...

Thread view: 
Roberto Nicastro - 11 May 2007 18:51 GMT
Hi all,
i have this piece of code

Suppose that lstA  is  List of 3 Objects

List<Object> lstA = ...something;
List<Object> lstB = lstA;
...
[block A]  //this is a block of code
...

Inside block A i remove one item from lstA, so lstA become a List of 2
Objects. Inside block A i never touch lstB.
Why at the end of block A also lstB is with 2 elements? How is this
possible?

Thanks in advance

R
Patricia Shanahan - 11 May 2007 19:11 GMT
> Hi all,
> i have this piece of code
[quoted text clipped - 11 lines]
> Why at the end of block A also lstB is with 2 elements? How is this
> possible?

Neither lstA nor lstB is a list. They are both reference variables that
are initialized as pointers to the same List object.

Although block A does not touch lstB, it does remove an element from the
List it points to.

Patricia
Richard Senior - 11 May 2007 19:17 GMT
> Suppose that lstA  is  List of 3 Objects
>
[quoted text clipped - 8 lines]
> Why at the end of block A also lstB is with 2 elements? How is this
> possible?

Because lstA and lstB are pointers. All objects are. When you assign
lstB = lstA, you make lstB point at the same list; you make lstB ==
lstA. Perhaps what you wanted to do was make lstB a copy of lstA? Or
perhaps you wanted to go further and make lstB a copy of lstA and all
its members copies of the members in lstA? Have you thought about what
happens if you change an object in either list?

Signature

Regards,

Richard

Roberto Nicastro - 12 May 2007 11:11 GMT
> lstA. Perhaps what you wanted to do was make lstB a copy of lstA? Or
> perhaps you wanted to go further and make lstB a copy of lstA and all
[quoted text clipped - 5 lines]
>
> Richard

I want to create an exact copy of lstA. So, to create a real copy i
have to create a new List object

List lstA = ...something
List lstB = new ArrayList();

for (Iterator it; it = lstA.iterator(); it.hasNext(); ){
    lstB.add(it.next());
}

I think this will work for me.
Thank you all

R
Richard Senior - 12 May 2007 11:34 GMT
> I want to create an exact copy of lstA. So, to create a real copy i
> have to create a new List object
[quoted text clipped - 7 lines]
>
> I think this will work for me.

Yes, but bear in mind that the two lists, while they are different
lists, contain the same elements. If you change an object in lstA, it
also changes in lstB.

Have a look at this
http://forum.java.sun.com/thread.jspa?threadID=487186&messageID=2281852
and do some searches on "shallow copy" and "deep copy".

Signature

Regards,

Richard

Jussi Piitulainen - 12 May 2007 13:54 GMT
> I want to create an exact copy of lstA. So, to create a real copy i
> have to create a new List object
[quoted text clipped - 7 lines]
>
> I think this will work for me.

There's an addAll method you could use instead of a loop of your own:

  List lstB = new ArrayList();
  lstB.addAll(lstA);

Look it up in Javadoc.
Patricia Shanahan - 12 May 2007 14:45 GMT
>> I want to create an exact copy of lstA. So, to create a real copy i
>> have to create a new List object
[quoted text clipped - 14 lines]
>
> Look it up in Javadoc.

Or, even simpler:

List lstB = new ArrayList(lstA);

Patricia
Raghav - 11 May 2007 19:18 GMT
> Hi all,
> i have this piece of code
[quoted text clipped - 15 lines]
>
> R

Actually here you have got one list in memory and 2 references to
that. So when u modify the list, it is reflected in either of
references pointing to that List.
HTH.
Eric Sosman - 11 May 2007 19:37 GMT
Roberto Nicastro wrote On 05/11/07 13:51,:
> Hi all,
> i have this piece of code
[quoted text clipped - 11 lines]
> Why at the end of block A also lstB is with 2 elements? How is this
> possible?

   An analogy (I love analogies!) that may help you
understand what others have pointed out: lstA is the
number on the debit card in your wallet, and lstB is
the same string of digits recorded in your password-
protected on-line form-filler assistant.  In [block A]
you hand the card to a local merchant to pay for your
new jPea, 150 silver splonders including VAT.  Then
you go to your computer, use the automatic form-filler
to log in to your bank account, and check the balance.
Are the 150 silver splonders still there to be spent?

   One bank account, two copies of the account number.
One List instance, two references to it.  Spend money
with the debit card, and the balance change is visible
when you use the copied number.  Change the List via
listA, and the change is visible through listB.

Signature

Eric.Sosman@sun.com

Chris Smith - 12 May 2007 14:48 GMT
> List<Object> lstA = ...something;
> List<Object> lstB = lstA;

While you've got several explanations of what's going on, you haven't
gotten much by way of how to make it act as you'd like.  To do that, you
could change the second line there to:

List<Object> lstB = new ArrayList(lstA); // or other List implementation

That would cause lstB to point to a different List, which has its
CONTENTS initialized from the list that lstA points to.  Then you could
modify the list that lstA points to, and the list that lstB points to
would be unchanged.

Signature

Chris Smith



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.