2

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

4 Answers 4

4

Since you're using Java 8, you could use the Stream API. Concat two streams of the original list, with one that maps each element by adding 10 to it.

List<Integer> arr = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> result = Stream.concat(arr.stream(), arr.stream().map(i -> i + 10))
                             .collect(Collectors.toList());

As you updated your question, I don't see why you can't create another list that you update continuously. This is what I'd do in your case.

Here's a solution that update the list you are iterating using an index-based for loop, but don't forget to cache the size before starting to iterate, so that you only process the elements that were in the list before the processing.

ArrayList<SomeClass> arr = new ArrayList<>();
int condition = 12; // example contidition number
final int prevSize = arr.size();
for (int i = 0; i < prevSize; i++) {
    SomeClass element = arr.get(i);
    if (element.conditionNumber == condition) {
        //probably update or create a new element here
        arr.add(someNewElement);
    } 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

It seems to me like a dangerous behavior, that may add elements indefinitely to the list, but you probably know the real problem you are facing. If you can try to avoid this that would be better, IMO.

Sign up to request clarification or add additional context in comments.

3 Comments

An alternative would be to use arr.addAll(arr.stream().map(i -> i+10).collect(Collectors.toList()). This does not recreate the original array (probably this matters in authors case).
@TagirValeev Yes if you want to modify the list in place this is probably the way to go.
I have changed my question, my explanation was poor. Maybe you understand my problem now...
0

Try:

This is in addition to what dasblinkenlight says: as add will add to its current position. you need to use

ArrayList<Integer> arr = new ArrayList<>(Arrays.asList(new Integer[]{1,2,3,4,5}));

ArrayList<Integer> arr1 =new ArrayList<Integer>();

for (ListIterator<Integer> i = arr.listIterator(); i.hasNext();) {          
    arr1.add(i.next() + 10);               
}       

arr.addAll(arr1);
System.out.println(arr);

Comments

0

Why not using plain old for?

ArrayList<SomeClass> arr = new ArrayList<>();               
int condition = 12; // example contidition number
for (int i = 0; i < arr.size(); i++) {
    if (arr.get(i).conditionNumber == condition) {
        // add to final of the list to process after all elements.
        arr.add(someNewElement);
    } else {
        // process the element.
        // change the contidition
        condition = 4; // another example, this number will change many times
    }                           
}

Not so cool, but should work in your case.

2 Comments

You'd need to cache the size before the loop, otherwise you may add elements indefinitely.
Nevermind I re-read the OP's question, "I can't create a separated list because the elements add to final may enter in the condition again". Seems like a dangerous behavior.
0

You could use the collect method which can join two list with your customization, like the one I have done below

  List<Integer> arr = Arrays.asList(1,2,3,4,5);                                                                

  List<Integer> newList = arr.stream()                                                                         
          .collect(ArrayList<Integer>::new, (list, num) -> {                                                   
              list.add(num);                                                                                   
              list.add(num + 10);                                                                              
          }, ArrayList::addAll)       // this will give us the final list of element num and num+10            
          .stream()                                                                                            
          .sorted()                                                                                            
          .collect(Collectors.toList());                                                                       

  System.out.println(newList );      

key here is using the below variation of collect method of stream api

collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator,BiConsumer<R, R> combiner);
Please let me know if it can solve the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.