Solved

Object tracking ( again )

Posted on 2004-09-02
5
180 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

813 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now