Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Creating new objects in for loop

Reply
Thread Tools

Creating new objects in for loop

 
 
nemrac98@gmail.com
Guest
Posts: n/a
 
      11-04-2005
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!

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2005
On 4 Nov 2005 05:36:59 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      11-04-2005
(E-Mail Removed) 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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      11-04-2005
On 4 Nov 2005 05:36:59 -0800, (E-Mail Removed) 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.
 
Reply With Quote
 
iamfractal@hotmail.com
Guest
Posts: n/a
 
      11-04-2005

(E-Mail Removed) 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.

 
Reply With Quote
 
chris_k
Guest
Posts: n/a
 
      11-04-2005
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

 
Reply With Quote
 
nemrac98@gmail.com
Guest
Posts: n/a
 
      11-04-2005
That was it! Wasn't recreating the temp array in the right place.
Thanks everyone I really appreciate it!

 
Reply With Quote
 
nemrac98@gmail.com
Guest
Posts: n/a
 
      11-04-2005
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!

 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Creating new Objects in loop - Java Arans Java 0 12-14-2011 02:41 PM
creating garbage collectable objects (caching objects) News123 Python 7 06-29-2009 04:12 PM
Creating Class Objects in Loop Fish Python 3 03-31-2008 03:51 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM



Advertisments