Solved

Object tracking ( again )

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 135
Problem to start Neon 20 86
HSSFWorkbook cannot be resolved error 10 50
java jdbc batch example 8 21
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

910 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

20 Experts available now in Live!

Get 1:1 Help Now