dynamic variables

N

NAJH

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
 
I

info

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:

for (int i = 0; i <20; i++)
{
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.
 
D

Danno

NAJH said:
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


Use a map...

HashMap<String, SpecialObject> map = new HashMap<SpecialObject> ();
for (int i = 0; i < 20; i++) {
map.put("myObj" + i, new SpecialObject());
}
 
L

Lasse Reichstein Nielsen

NAJH said:
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 = 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
 
P

Patricia Shanahan

NAJH said:
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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top