help with nested for

D

Denis

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
 
V

Venky

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).
 
D

Denis

Not so good... when I create myObject, the param order is important!
Width your solution the param order change! :)

DM
 
V

Venky

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.
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top