Issue
i have a class:
public class item{
TypeEnum type;
double value;
}
public enum TypeEnum {
max,
min
}
i have an ArrayList of items like so : {min, max, max ,max, min, min, min, max, min}
each item of the list is an object with {type(min/max), value}.
and i want to transform it to : {min, max, min, max} (with keeping the same order just replace the max, max, max ===> the biggest one instead based on the value ).
i want to replace the suit of max with only the biggest one, and suit of min with smallest one.
Solution
You could try and do this "in-place", modifying your original list, but it will be much simpler to create a new list, adding the min or max of each sequence.
System.out.println(items);
List<Item> newItems = new ArrayList<>();
for(int i=0; i<items.size(); )
{
Item pick = items.get(i);
for(i += 1; i < items.size() && pick.type == items.get(i).type; i++)
{
if((pick.value > items.get(i).value) == (pick.type == TypeEnum.MIN))
pick = items.get(i);
}
newItems.add(pick);
}
System.out.println(newItems);
Output:
[(MIN:4), (MAX:1), (MAX:5), (MAX:3), (MIN:7), (MIN:4), (MIN:1), (MAX:6), (MIN:5)]
[(MIN:4), (MAX:5), (MIN:1), (MAX:6), (MIN:5)]
Answered By - RaffleBuffle