The Artima Developer Community
Sponsored Link

Java Answers Forum
How can i break the reference

5 replies on 1 page. Most recent reply: Apr 25, 2003 9:09 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Ken

Posts: 7
Nickname: ken85
Registered: Apr, 2003

How can i break the reference Posted: Apr 22, 2003 10:19 AM
Reply to this message Reply
Advertisement
hello everyone, for my understanding if i set
int size = arrayList.size();
and when i add an addional object to the list..
arrayList.add(anObject);
size will refer to the new arrayList.size();
how can i stop this referring, because i need to get the size at the point of time and compare to the new arrayList.size() later on ..thank yoU!


Ken

Posts: 7
Nickname: ken85
Registered: Apr, 2003

Re: How can i break the reference Posted: Apr 22, 2003 11:22 AM
Reply to this message Reply
opss Sorry i realise tat i am wrong, primitive type does not refer to Object .. or can i rephrase my question, how can i create a new copy of object other than referring

scott

Posts: 1
Nickname: thpopes
Registered: Apr, 2003

Re: How can i break the reference Posted: Apr 22, 2003 4:45 PM
Reply to this message Reply
If you want a list that contains references to all of the objects in you original list you can do the following.

Object a = new Object();
Object b = new Object();
List list = new ArrayList();
list.add(a);
list.add(b);

List l2 = new ArrayList(list);

System.out.println(a.equals(l2.get(0)));
System.out.println(b.equals(l2.get(1)));

If you make any changes to either Object a or Object b (which you can't in the case of the Object class) the changes will be reflected in both lists.

if you want a "disconnected" copy of the first list you will have to extend some effort to "clone" the objects in your list.

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: How can i break the reference Posted: Apr 22, 2003 11:51 PM
Reply to this message Reply
> How can i create a new copy of object other than referring?

It sounds like a simple enough thing to want to do. Unfortunately, Java does not make it easy for you. As mentioned above, you need to override the Object.clone() method. A full explanation of the ins and outs of doing so is a bit much to cram into a forum such as this. I suggest you download Bruce Eckel's book 'Thinking in Java' (http://mindview.net/Books/TIJ/DownloadSites) and have a read of Appendix A.

Ken

Posts: 7
Nickname: ken85
Registered: Apr, 2003

Re: How can i break the reference Posted: Apr 25, 2003 8:56 AM
Reply to this message Reply
Thank you for enlightening me!

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: How can i break the reference Posted: Apr 25, 2003 9:09 AM
Reply to this message Reply
Josh Bloch's Effective Java book also has really excellent coverage of cloning and copy constructors.

Flat View: This topic has 5 replies on 1 page
Topic: Creating a new instance of a class Previous Topic   Next Topic Topic: palindrom function

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use