I have arrays: arrAand arrB. arrAand arrB- these are lists of objects of different types, and the function addconverts the objects Ainto objects B. I want to add every object from arrA to arrB and remove this object from arrA. I am trying to do this downstream:
arrA.stream().foreach(c -> {arrB.add(c); arrA.remove(c);});
when I do this, two things happen:
- not all objects are passed from arrA to arrB.
- after several iterations, null pointer exception.
i believes that the length of the array decreases after each call remove(), and the iteration counter increases (only objects with odd indices are passed to arrB)
Now I can solve this by copying the array in one stream call, and then delete the objects in the second stream call, but this does not seem right for me.
What would be the correct solution to this problem?
EDIT. Additional information: in a real implementation, this list, if previously filtered
arrA.stream().filter(some condition).foreach(c -> {arrB.add(c); arrA.remove(c);});
and its called several times to add elements that satisfy different conditions in different lists ( arrC, arrDetc.), but each object can be in only one list
source
share