Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > dynamic variables

Reply
Thread Tools

dynamic variables

 
 
NAJH
Guest
Posts: n/a
 
      07-22-2006
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

 
Reply With Quote
 
 
 
 
info@hmdproducts.com
Guest
Posts: n/a
 
      07-22-2006
> 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.

 
Reply With Quote
 
 
 
 
Danno
Guest
Posts: n/a
 
      07-22-2006

NAJH wrote:
> 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());
}

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-22-2006
"NAJH" <(E-Mail Removed)> writes:

> 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
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      07-23-2006
NAJH wrote:
> 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
 
Reply With Quote
 
NAJH
Guest
Posts: n/a
 
      07-23-2006
Many thanks for all of your replies on this.
Cheers,
Neil

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Session Variables and Static Variables cobus.lombard@gmail.com ASP .Net 1 03-26-2006 11:05 AM
Accessing class member variables - properties or variables? dwok Java 7 03-04-2005 03:54 AM
Dynamic Variables? OR Dynamic Controls =?Utf-8?B?VGVyb3M=?= ASP .Net 1 08-10-2004 01:13 PM
Re: Class public shared Variables vs. Application Variables in ASP.NET avnrao ASP .Net 0 05-07-2004 05:28 AM



Advertisments