dynamic variables

Discussion in 'Java' started by NAJH, Jul 22, 2006.

  1. NAJH

    NAJH Guest

    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
    NAJH, Jul 22, 2006
    #1
    1. Advertising

  2. NAJH

    Guest

    > 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.
    , Jul 22, 2006
    #2
    1. Advertising

  3. NAJH

    Danno Guest

    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());
    }
    Danno, Jul 22, 2006
    #3
  4. "NAJH" <> 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 = 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jul 22, 2006
    #4
  5. 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
    Patricia Shanahan, Jul 23, 2006
    #5
  6. NAJH

    NAJH Guest

    Many thanks for all of your replies on this.
    Cheers,
    Neil
    NAJH, Jul 23, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. avnrao
    Replies:
    0
    Views:
    3,362
    avnrao
    May 7, 2004
  2. =?Utf-8?B?VGVyb3M=?=

    Dynamic Variables? OR Dynamic Controls

    =?Utf-8?B?VGVyb3M=?=, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    419
    Scott Allen
    Aug 10, 2004
  3. Replies:
    1
    Views:
    931
  4. dwok
    Replies:
    7
    Views:
    697
    Hal Rosser
    Mar 4, 2005
  5. Replies:
    9
    Views:
    935
Loading...

Share This Page