I want to add some elements to an arrayList during iteraction, but I want to add in the final of the list, I have tried with ListIterator but only adds after the actual element on interaction...
Example:
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5}));
for (ListIterator<Integer> i = arr.listIterator(); i.hasNext();) {
i.add(i.next() + 10);
}
System.out.println(arr);
That prints: [1, 11, 2, 12, 3, 13, 4, 14, 5, 15]
What I have to do to get: [1, 2, 3, 4, 5, 11, 12, 13, 14, 15] ?
My problem cannot be solved creating another list and using addAll() after... My explanation of the problem was poor, let me explain better:
ArrayList<SomeClass> arr = new ArrayList<>();
int condition = 12; // example contidition number
for (ListIterator<SomeClass> i = arr.listIterator(); i.hasNext();) {
if (i.next().conditionNumber == condition) {
// add to final of the list to process after all elements.
} else {
// process the element.
// change the contidition
condition = 4; // another example, this number will change many times
}
}
I can't create a separated list because the elements add to final may enter in the condition again
Thanks