I'm a newbie and therefore have an unfortunately dumb question...
In some languages I've used I've been able to create or reference
variables in a dynamic way, in order to build them up formulaicly...
I'd like to know how to do that in Java. So lets say I had a for loop
and I wanted to create 20 objects with names myObj1...myObj20:
I'd do...
for (int i = 0; i <20; i++)
{
SpecialObject "myObj" + i = new SpecialObject();
}
...but obviously that's not right, because I just made that up, but can
you see what I'm trying to acheive?
Many thanks!
Neil
info@hmdproducts.com - 22 Jul 2006 21:52 GMT
> In some languages I've used I've been able to create or reference
> variables in a dynamic way, in order to build them up formulaicly...
[quoted text clipped - 6 lines]
> SpecialObject "myObj" + i = new SpecialObject();
> }
If 20 was 'dynamic' you could do:
int limitDynamic = 20;
SpecialObject[] mySpecialObjectArray = new SpecialObject[limitDynamic];
That's somewhat similiar to what you're saying in your example.
A better way is to use ArrayList, Vector, or HashMap from the java.util
package. Something like:
java.util.ArrayList<SpecialObject> specialObjectArrayList = new
java.util.ArrayList<SpecialObject>();
If you want name associations, "myObj" etc., you can accomplish that
with HashMaps by providing the name in the put() method.
Danno - 22 Jul 2006 22:01 GMT
> I'm a newbie and therefore have an unfortunately dumb question...
>
[quoted text clipped - 16 lines]
> Many thanks!
> Neil
Use a map...
HashMap<String, SpecialObject> map = new HashMap<SpecialObject> ();
for (int i = 0; i < 20; i++) {
map.put("myObj" + i, new SpecialObject());
}
Lasse Reichstein Nielsen - 22 Jul 2006 22:03 GMT
> In some languages I've used I've been able to create or reference
> variables in a dynamic way, in order to build them up formulaicly...
> I'd like to know how to do that in Java. So lets say I had a for loop
> and I wanted to create 20 objects with names myObj1...myObj20:
Creating 20 objects and making them referencable using names does not
have to involve variables (and really shouldn't).
> I'd do...
>
> for (int i = 0; i <20; i++)
> {
> SpecialObject "myObj" + i = new SpecialObject();
> }
Apart from it not working, how would you ever refer to these objects
again? Very probably, you would also do this using computed names.
In that case, you just need something to hold the mapping from name
to object:
Map<SpecialObject> store = new HashMap<SpecialObject>():
for (int i = 0; i < 20; i++) {
store.put("myObj"+i, new SpecialObject());
}
and then you can refer to it again using
store.get("myObj"+j)
where you need it.
Then again, using symbolic names in strings is overkill. You could just
do:
SpecialObject[] store = new SpecialObject[20];
for (int i = 0; i < 20; i++) {
store[i] = new SpecialObject();
}
and reference it as:
store[j]
> ...but obviously that's not right, because I just made that up, but can
> you see what I'm trying to acheive?
I hope I do. You need to create a variable number of objects and refer
to them later. Variables is not a good way to do this, in any
language.
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Patricia Shanahan - 23 Jul 2006 02:49 GMT
> I'm a newbie and therefore have an unfortunately dumb question...
>
[quoted text clipped - 16 lines]
> Many thanks!
> Neil
I agree with the previous replies suggesting arrays, maps etc., and just
want to add a couple of comments.
That sort of dynamic variable use is actually a hidden use of a hash
table with a string key, and the value of the variable as value.
An implementation for a language that does allow dynamic variables
typically has a hash table that maps variable name to data about the
variable, including its memory location.
The most direct Java equivalent would be a hash table with the
"variable" name as key. However, if you really want an integer range of
them, consider using an array.
Patricia
NAJH - 23 Jul 2006 15:36 GMT
Many thanks for all of your replies on this.
Cheers,
Neil