I am trying to get a list of subsets that sum up to the specified sum and I keep running out if index. Below is my code:
private void calcBudget(List<ListInfo> data, int fromIndex, int endIndex, int sumInStack, float target) {
AppUtils.log(TAG, "Begining: " + fromIndex);
if (sumInStack == target) {
AppUtils.log(TAG, "TotalSize: " + stackInfo.size());
}
for (int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++) {
ListInfo info = data.get(currentIndex);
Price currentPrice = info.getPrices().get(0);
if (sumInStack + currentPrice.getPrice() <= target) {
stackInfo.add(data.get(currentIndex));
sumInStack += currentPrice.getPrice();
calcBudget(data, currentIndex+1, endIndex, sumInStack, target);
Price toPopPrice = data.get(0).getPrices().get(0);
sumInStack -= toPopPrice.getPrice();
data.remove(0);
}
}
}
And the exception is always thrown at last but two. E.g: If length == 10 it throws if currentIndex == 8.
I keep getting IndexOutOfBoundException. After some time here ListInfo info = data.get(currentIndex);
I don't know why. Any help would be appreciated.
EDIT:
this is the way I am calling the method:
calcBudget(listInfos, 0, listInfos.size() - 1, 0, 45);