We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › How do objects in an ArrayList get status updates
Page Index Toggle Pages: 1
How do objects in an ArrayList get status updates? (Read 494 times)
How do objects in an ArrayList get status updates?
May 27th, 2009, 9:34am
 
This is just a curiosity question. If I have code looking something like this:

Code:

ArrayList things = new ArrayList();
things.add(new Thing());
Thing thing = (Thing)things.get(i);
thing.doStuff();


I've noticed that you don't have to include Code:
things.set(0,thing); 

at the end to update your original object in the ArrayList with the local object you just messed with. Your original object in the ArrayList is simply updated accordingly. I never quite understood why that happens. It's about time I find out.  Wink Can someone fill me in?
Re: How do objects in an ArrayList get status updates?
Reply #1 - May 27th, 2009, 9:46am
 
The reason you don't have to set the value is because the array itself doesn't actually hold the objects. It holds a reference to the object. A shortcut, if you will. If the 3rd item in an array is an object called Ball, then

blargle = (Thing)things.get(3);
blargle.x = 10;

is the same as just inputing

Ball.x = 10;

Now for this example you're updating an x field, which will be used during your draw() loop or something.

But the point is, you don't have to use set() for the arraylist for the same reason you don't have to use set() for Dude.x = sin(mouseX);

I think that's your question... if not, ignore me.
Re: How do objects in an ArrayList get status updates?
Reply #2 - May 27th, 2009, 9:56am
 
No, that's perfect. Thanks for the reply. I should have realized myself that it's a reference. Kind of like vectors in C++.
Page Index Toggle Pages: 1