Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2005

Tip: Looking for answers? Try searching our database.

Creating new objects in for loop

Thread view: 
nemrac98@gmail.com - 04 Nov 2005 14:36 GMT
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!
Roedy Green - 04 Nov 2005 15:11 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Robert Klemme - 04 Nov 2005 15:14 GMT
> 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
[quoted text clipped - 20 lines]
> for(int j = 0; j < numOfRegions; j++)
>          {

Create tmp array here!

>             for(int i = 0; i < stop; i++)
>             {
[quoted text clipped - 8 lines]
> 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
nemrac98@gmail.com - 04 Nov 2005 15:22 GMT
That was it! Wasn't recreating the temp array in the right place.
Thanks everyone I really appreciate it!
nemrac98@gmail.com - 04 Nov 2005 15:24 GMT
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!
Roedy Green - 04 Nov 2005 15:14 GMT
>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.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

iamfractal@hotmail.com - 04 Nov 2005 15:15 GMT
> I have a piece of code which I would like to make a new object with a

> for(int j = 0; j < numOfRegions; j++)
>          {
[quoted text clipped - 6 lines]
>             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.
chris_k - 04 Nov 2005 15:21 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.