?
Solved

Object tracking ( again )

Posted on 2004-09-02
5
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
expertmb earned 260 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 120 total points
ID: 11962140
yup
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 260 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 120 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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