Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • Last Modified:

Object tracking ( again )

Hi,
  A while ago when I was first getting in to Java I asked you guys the best way to track objects - which is the good old ArrayList. This worked perfectly as all I needed to do was read data from the objects and so I was happy and content. Today though, I need to write back to the objects in the array. I've got this so far :


Object test = (Object)arrayList.get(0);


My question is whether or not this creates another copy or whether it's a reference to the object that already exists. I think it's a reference as I'm not using "new" to instantiate a new one.... but I figured I'd ask because even if I test it, my test might be flawed, leading me to the wrong conclusion.... so once again I have to ask for your assistance :)

0
petepalmer
Asked:
petepalmer
  • 2
  • 2
4 Solutions
 
expertmbCommented:
>>whether it's a reference to the object that already exists.
it's a reference to the object that already exists.
0
 
TimYatesCommented:
yup
0
 
expertmbCommented:
>> I need to write back to the objects in the array.

you can do like this also
Object[] object = arrayList.toArray();
0
 
zzynxSoftware engineerCommented:
>> it's a reference to the object that already exists.
Indeed. So you can change it and the changes will take place in the list.

>>Object test = (Object)arrayList.get(0);
I guess you mean

       MyOwnObject test = (MyOwnObject)arrayList.get(0);

       test.myFunction();  // will adapt the entry in the list
0
 
zzynxSoftware engineerCommented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now