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 / January 2006

Tip: Looking for answers? Try searching our database.

help with nested for

Thread view: 
Denis - 17 Jan 2006 09:05 GMT
Hi all.
I hope you can understand my poor english...

I've two for cycles nested
for (vector1, index i)
  for (vector2, index j)
     myObject o = new myObject(vector1.elementAt(i),
vector2.elementAt(j))

The problem is that the order of the nested cycles depends of a param.
I could use something like

if (param == true)
  x = vector1
  y = vector2
else
  x = vector2
  y = vector1

for (x, index i)
  for (y, index j)
     if (param == true)
        myObject o = new myObject(x.elementAt(i), y.elementAt(j))
     else
        myObject o = new myObject(y.elementAt(i), x.elementAt(j))

but is there a standard or better method to do this?

Thanks all!
DM
Venky - 17 Jan 2006 09:21 GMT
I think the solution which you have mentioned is good if you remove the
if condition in the for block as you have already taken care of it
outside the for block.

Anyways, You can do this in two ways
1)
if (param == true) {
  x = vector1
  y = vector2
}else {
  x = vector2
  y = vector1
}
for (x, index i)
  for (y, index j)
       myObject o = new myObject(x.elementAt(i), y.elementAt(j))

2)
for (vector1, index i)
  for (vector2, index j)
     if (param == true)
        myObject o = new myObject(vector1.elementAt(i),
vector2.elementAt(j))
     else
        myObject o = new myObject(vector2.elementAt(i),
vector1.elementAt(j))

I think first one is better because of less number of comparisions
(even though you are using two extra variables).
Denis - 17 Jan 2006 09:41 GMT
Not so good... when I create myObject, the param order is important!
Width your solution the param order change! :-)

DM
Venky - 17 Jan 2006 10:05 GMT
If the value of "param" variable doesn't change in the for loop, then
both the solutions which I have mentioned above will give the same
result as yours.


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.