Solved

Object tracking ( again )

Posted on 2004-09-02
5
178 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
Comment Utility
>>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
Comment Utility
yup
0
 
LVL 6

Assisted Solution

by:expertmb
expertmb earned 65 total points
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
Thanks :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
copyEndy  challenge 15 55
network + 7 73
firstChar challenge 13 83
Core Java. What output will be and why ? 6 57
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 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

11 Experts available now in Live!

Get 1:1 Help Now