1

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.

Hubi
  • 440
  • 1
  • 11
  • 25

2 Answers2

1

Even though you don't change the original lists, you change the objects in those lists. You need to make a deep copy of nextMonthElementTemp, depending on what the class AccountlinevaluesEntity looks like, this can be done in different ways.

But in the most basic scenario, you need to replace this line:
AccountlinevaluesEntity firstMonthElement = firstMonth.get(k);
With something like this:
AccountlinevaluesEntity firstMonthElement = firstMonth.get(k).clone();

Lazar Petrovic
  • 537
  • 3
  • 8
  • 1
    To express it differently: Java `List` does not contain objects; it contains *references* to objects; [those are different](https://stackoverflow.com/questions/10766681/what-is-the-difference-between-an-object-reference-id-and-reference-variable). – Raedwald Dec 02 '19 at 12:29
  • I implemented the cloanble interface in the object class. works very well. To get a better understanding I did some research here: https://stackoverflow.com/questions/4081858/about-java-cloneable and here: https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html Thanks for the tip :) – Hubi Dec 02 '19 at 13:03
0

You will need to use additional lists. You can either store result of the operations in new lists, or create a snapshot of your list if you need to keep its state. So for this particular situation, I would never change the state of the initial lists as you still need their values.

  • 1
    That's the point, I don't want to change state of the initial lists. Therefore List tempList = new ArrayList<>(a_l_v_eList.get(i + 1)); but is apparently the wrong approach. – Hubi Dec 02 '19 at 11:59
  • 1
    You are not really keeping the state of your list of objects, you are just keeping the references to the objects. Try to use [Collections.copy(destination, source);](https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#copy%28java.util.List,%20java.util.List%29) – Gavin Stacey Dec 02 '19 at 12:29