Reputation: 2380
I am finding the largest difference between the arrayList minuteList
element then am incrementing the element with min_value
after this position to refill the timetable. With the current the same element is always being added.
minuteList bevore the while statement:
[288, 318, 346, 376, 406, 436, 466, 1006, 1036, 1066, 1096, 1126, 1156]
minuteList` after a few loops:
[288, 318, 346, 376, 406, 436, 466, 496, 496, 496, 496, 1006, 1036, 1066, 1096, 1126, 1156]
Correct result should looks like
[288, 318, 346, 376, 406, 436, 466, 496, 526, 556, 586, 616,..., 1006, 1036, 1066, 1096, 1126, 1156]
Code:
Collections.sort(diffArray);
int element = diffArray.get(diffArray.size() - 1).getElement();
int nextElement = diffArray.get(diffArray.size()-1).getNextElement();
int differance = nextElement - element;
int elementIndex = minuteList.indexOf(element);
while(differance > min_value){
int addTime = minuteList.get(elementIndex) + min_value;
minuteList.add(elementIndex + 1, addTime);
}
Upvotes: 1
Views: 370
Reputation: 393831
Your loop keeps adding the same value (minuteList.get(elementIndex) + min_value
) to the same position (elementIndex + 1
) of the ArrayList
, which explains the multiple 496 elements you see.
If you wish a new value to be added in each iteration, you need addTime
to change in each iteration :
int addTime = minuteList.get(elementIndex) + min_value;
while(differance > min_value){
minuteList.add(++elementIndex, addTime);
addTime += min_value;
}
Note that I'm also incrementing elementIndex
. Otherwise, all the new elements will be added at the same position, pushing the previously added elements forward, so they'll be added in reverse order.
You might want to add some code inside the loop that actually modifies differance
or min_value
. Otherwise the loop will never end.
Upvotes: 1