Creating new objects in for loop

Discussion in 'Java' started by nemrac98@gmail.com, Nov 4, 2005.

  1. Guest

    I have a piece of code which I would like to make a new object with a
    different variable name everytime I loop through a for loop. Right now
    I am using the same variable name and writing over it everytime but
    before an iteration happens in the loop I save the object to an array.
    The problem is it doesn't seem to be working. I am using OpenMap to
    display a region so it could be something with OpenMap.

    If I do:

    RegionLayer region1 = new RegionLayer(layerArray);
    RegionLayer region2 = new RegionLayer(layerArray);
    RegionLayer region3 = new RegionLayer(layerArray);

    mapHandler.add(region1);
    mapHandler.add(region2);
    mapHandler.add(region3);

    ...and then add them to the map one after the other it works fine and it
    displays all three of the regions on the map.

    However if I do something like the following:

    for(int j = 0; j < numOfRegions; j++)
    {
    for(int i = 0; i < stop; i++)
    {
    ***Make tempArray to send as argument to RegionLayer***
    }

    RegionLayer region = new RegionLayer(tempArray);
    mapHandler.add(region);
    }

    This causes only the last region added to show up on my map. Any help
    or advice is appreciated, I am still getting the hang of things. Thanks
    in advance for any help!
    , Nov 4, 2005
    #1
    1. Advertising

  2. Roedy Green Guest

    On 4 Nov 2005 05:36:59 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >I have a piece of code which I would like to make a new object with a
    >different variable name everytime I loop through a for loop.


    Variable names in Java are fixed at compile time. You cannot generate
    new ones at run time.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 4, 2005
    #2
    1. Advertising

  3. wrote:
    > I have a piece of code which I would like to make a new object with a
    > different variable name everytime I loop through a for loop. Right now
    > I am using the same variable name and writing over it everytime but
    > before an iteration happens in the loop I save the object to an array.
    > The problem is it doesn't seem to be working. I am using OpenMap to
    > display a region so it could be something with OpenMap.
    >
    > If I do:
    >
    > RegionLayer region1 = new RegionLayer(layerArray);
    > RegionLayer region2 = new RegionLayer(layerArray);
    > RegionLayer region3 = new RegionLayer(layerArray);
    >
    > mapHandler.add(region1);
    > mapHandler.add(region2);
    > mapHandler.add(region3);
    >
    > ..and then add them to the map one after the other it works fine and
    > it displays all three of the regions on the map.
    >
    > However if I do something like the following:
    >
    > for(int j = 0; j < numOfRegions; j++)
    > {


    Create tmp array here!

    > for(int i = 0; i < stop; i++)
    > {
    > ***Make tempArray to send as argument to
    > RegionLayer*** }
    >
    > RegionLayer region = new RegionLayer(tempArray);
    > mapHandler.add(region);
    > }
    >
    > This causes only the last region added to show up on my map. Any help
    > or advice is appreciated, I am still getting the hang of things.
    > Thanks in advance for any help!


    You're likely not recreating the temp array, so all mapHandler.add()
    methods have the same argument and thus only the last element put into the
    array survives.

    robert
    Robert Klemme, Nov 4, 2005
    #3
  4. Roedy Green Guest

    On 4 Nov 2005 05:36:59 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >I have a piece of code which I would like to make a new object with a
    >different variable name everytime I loop through a for loop. Right now
    >I am using the same variable name and writing over it everytime but
    >before an iteration happens in the loop I save the object to an array.
    >The problem is it doesn't seem to be working. I am using OpenMap to
    >display a region so it could be something with OpenMap


    It will be hard for anyone to understand your program without knowing
    what a RegionLayer is or a MapHandler or a layerArray.

    If at all possible see if you can compose a COMPLETE program that
    demonstrates your problem.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 4, 2005
    #4
  5. Guest

    wrote:
    > I have a piece of code which I would like to make a new object with a


    >


    > for(int j = 0; j < numOfRegions; j++)
    > {
    > for(int i = 0; i < stop; i++)
    > {
    > ***Make tempArray to send as argument to RegionLayer***
    > }
    >
    > RegionLayer region = new RegionLayer(tempArray);
    > mapHandler.add(region);
    > }
    >


    (Without seeing all the code) this looks like a perfectly valid
    approach. On each iteration, a new RegionLayer object is being created
    and add to the map. The new RegionLayer object created on the next
    iteration will not interfere with the RegionLayer object created on the
    previous iteration; they are separate, and both "should" be added to
    the map despite both being instantiated with the same name.

    I would suspect your code is misbehaving in some other respect.

    ..ed

    --
    www.EdmundKirwan.com - Home of The Fractal Class Composition.
    , Nov 4, 2005
    #5
  6. chris_k Guest

    Hi,

    try something like :

    for(int j = 0; j < numOfRegions; j++)
    {
    tempArray[] = new tempArray[stop];
    for(int i = 0; i < stop; i++)
    {
    *** fill tempArray
    }

    RegionLayer region = new RegionLayer(tempArray);
    mapHandler.add(region);
    }

    HTH
    chris
    chris_k, Nov 4, 2005
    #6
  7. Guest

    That was it! Wasn't recreating the temp array in the right place.
    Thanks everyone I really appreciate it!
    , Nov 4, 2005
    #7
  8. Guest

    Thank you for all your help everyone...sounds like everyone was
    pointing me in the right direction, simply needed to recreate the temp
    array inside the inner loop. I appreciate it guys thanks a bunch!
    , Nov 4, 2005
    #8
    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. 7stud
    Replies:
    11
    Views:
    689
    Dennis Lee Bieber
    Mar 20, 2007
  2. Fish
    Replies:
    3
    Views:
    1,375
    D'Arcy J.M. Cain
    Mar 31, 2008
  3. News123
    Replies:
    7
    Views:
    379
    Dave Angel
    Jun 29, 2009
  4. Arans
    Replies:
    0
    Views:
    589
    Arans
    Dec 14, 2011
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page