Solved

Object tracking ( again )

Posted on 2004-09-02
5
181 Views
Last Modified: 2010-03-31
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
Comment
Question by:petepalmer
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
expertmb earned 65 total points
ID: 11962129
>>whether it's a reference to the object that already exists.
it's a reference to the object that already exists.
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 30 total points
ID: 11962140
yup
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 65 total points
ID: 11962151
>> I need to write back to the objects in the array.

you can do like this also
Object[] object = arrayList.toArray();
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 30 total points
ID: 11962193
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 11962236
Thanks :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 104
diffSum example 4 50
Way to decrease size of apk file 9 86
how to install java on RHEL image on EC2 4 40
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question