O
obaqueiro
Hello, so I am trying to make a better (with less complexity,
prefferably without a nested loop) to find something like pairs of
items in an ArrayList:
So far I've got this:
// shuffle Option offers
Collections.shuffle(offeredOptions);
for (int i=0;i<offeredOptions.size()-1;i++)
{
Offer of1 = offeredOptions.get(i);
// look for a matching option offer in the list
for (int j=1;j<offeredOptions.size();j++)
{
Offer of2 = offeredOptions.get(j);
/* we are looking for 2 offers for the same Option contract with
opposite
* offer types (write and hold)
*/
if (of1.getOption() == of1.getOption() &&
(of1.type == OfferType.hold && of2.type == OfferType.write) ||
(of1.type == OfferType.write && of2.type == OfferType.hold))
{
offeredOptions.remove(i);
offeredOptions.remove(j);
// do something else
....
// ...
break; // get to the
i loop
}
}
}
I know it is not optimal, but basically I need to match 2 items from
the array (which is randomized), and then remove them.
I am sure there must be a more efficient way to do this but I cant
remember the proper algorithm...
I know it is not a *proper* Java question but didn't knew which group
to address
thanks anyway!
prefferably without a nested loop) to find something like pairs of
items in an ArrayList:
So far I've got this:
// shuffle Option offers
Collections.shuffle(offeredOptions);
for (int i=0;i<offeredOptions.size()-1;i++)
{
Offer of1 = offeredOptions.get(i);
// look for a matching option offer in the list
for (int j=1;j<offeredOptions.size();j++)
{
Offer of2 = offeredOptions.get(j);
/* we are looking for 2 offers for the same Option contract with
opposite
* offer types (write and hold)
*/
if (of1.getOption() == of1.getOption() &&
(of1.type == OfferType.hold && of2.type == OfferType.write) ||
(of1.type == OfferType.write && of2.type == OfferType.hold))
{
offeredOptions.remove(i);
offeredOptions.remove(j);
// do something else
....
// ...
break; // get to the
i loop
}
}
}
I know it is not optimal, but basically I need to match 2 items from
the array (which is randomized), and then remove them.
I am sure there must be a more efficient way to do this but I cant
remember the proper algorithm...
I know it is not a *proper* Java question but didn't knew which group
to address
thanks anyway!