Creating new objects in for loop

N

nemrac98

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!
 
R

Roedy Green

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.
 
R

Robert Klemme

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
 
R

Roedy Green

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.
 
I

iamfractal

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
 
C

chris_k

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
 
N

nemrac98

That was it! Wasn't recreating the temp array in the right place.
Thanks everyone I really appreciate it!
 
N

nemrac98

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!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top