There are several lists in one list... Now I try to subtract values of the first list from the secound list... Values of the secound list from the third list... Values of the third list from the fourth list and so on..
Example:
List 1:
- 30, 20, 10
List 2:
- 25, 15, 5, 36
List 3:
- 36, 95, 14, 12, 44
So in this example we subtract 30 from list one of 25 from list two, 20 from list one of 15 from list two, 10 from list one of 5 from list two. Next step, we subtract 25 from List one of 36 from list three, 15 from list two of 95 from list three, 5 from list two of 14 from list three and 36 from list one of 12 from list three.
And this is how the result should look like:
List 1:
- 30, 20, 10
List 2:
- -5, -5, -5, 36
List 3:
- 11, 80, 9, -24, 44
These lists are an example of how it should work. In my following code it doesn't work the way I imagine it to.
for (int i = 0; i < a_l_v_eList.size() - 1; i++) {
List<AccountlinevaluesEntity> firstMonth = a_l_v_eList.get(i);
List<AccountlinevaluesEntity> tempList = new ArrayList<>(a_l_v_eList.get(i + 1));
for (int j = 0; j < tempList.size(); j++) {
AccountlinevaluesEntity nextMonthElementTemp = tempList.get(j);
for (int k = 0; k < firstMonth.size(); k++) {
AccountlinevaluesEntity firstMonthElement = firstMonth.get(k);
if (nextMonthElementTemp.getAccountNumber() == firstMonthElement.getAccountNumber()) {
nextMonthElementTemp.setAmount(subtractAmount(firstMonthElement.getAmount(), nextMonthElementTemp.getAmount()));
//i + 1 so, we don´t overwrite the first element
finalList.get(i + 1).add(nextMonthElementTemp);
break;
}
}
}
}
In the line List<AccountlinevaluesEntity> tempList = new ArrayList<>(a_l_v_eList.get(i + 1));
I get the next list. After the values have been calculated, they are added to a new list. Unfortunately, the next time the loop is run, it will no longer contain the original values of the previously next list, but the calculated values.
My problem is that the original values in the lists are probably overwritten.
Among other things I created the templist outside the for loop... and worked with List.copyOf(). But the problem was always the same.
I hope I could describe the problem in an understandable way and you can help me.