Click to See Complete Forum and Search --> : java: Hashtable, ArrayList, etc?


nanode
01-24-2001, 02:15 PM
I'm having trouble determining the best way to treat a collection of objects for my situation.

I will create instances of objects that get manipulated (property changes, etc.) during runtime. To keep track of all these objects, I'd like to use an array-like container so I can conveniently iterate through the entire collection and other nice stuff.

Problem is, I need to be able to manipulate values of the object from the container and the instance directly.


MyClass m = new MyClass();
arrayListX.add(m);

//need to do either way below
m.setName("bob");
((MyClass)arrayList.get(0)).setName("bob");


I'm partial to a Hashtable, since each object has a unique index #, which I could use as a key - and would save exhaustive searches on an array or vector.

Will the last 2 lines of my code block accomplish the same thing? My real code is much more complex, so not as easy to experiment.

Thanks.

Stuka
01-24-2001, 03:18 PM
I don't know for sure about Java, but to do something like that in C++ would require some casting based on the object type - like what you did. The only problem, of course, is if you have multiple object types - then they would have to support some sort of RTTI, which you could then use to make the appropriate cast. I think that the issues are similar in Java, but I have no idea how to solve them...sorry! http://www.linuxnewbie.org/ubb/frown.gif

nanode
01-24-2001, 03:51 PM
Stuka,

I can manage casting just fine - there are numerous ways, but that's not what I'm asking.

I need to know if both the object stored in a container and the object instance in my class are pointers to the same 'thing'.

So if I modify a value of the object from inside the container or the instance, will the respective values be in sync.?

Sterling
01-25-2001, 11:16 AM
Nanode - They SHOULD be. Remember that Java tries to avoid creating copies of objects unless it needs to. (Java references are actually quite nice, when you think about it. One of the things I like about the language.) You could always try throwing together a quick test program to check the details of what you need to do - I often find that to be VERY helpful.

------------------
-Sterling
"There is no Linuxnewbie.org cabal..."

nanode
01-25-2001, 11:33 AM
Sterling,

You're assumption is correct. The values themselves are in sync - or rather point to the same thing.

For some reason my GUI display of these values isn't updating, and the displayed value is getting re-assigned to the object, when I didn't expect it.

Thanks all.