In an ArrayList of Strings | Sololearn: Learn to code for FREE!


In an ArrayList of Strings

I want to duplicate/two copies each of any string that contains the letter 'l' . How shall i do that. My problem here is when i add the string i dont know how to update the index of the arraylist.

6/20/2020 4:09:56 AM

stephen haokip

6 Answers

New Answer


Where do you want to store the new copy of your string: 1) right next to the original string? or 2) at the end of the ArrayList?


The add method of the ArrayList class has another form. You can specify the index where you want to add the item by passing an integer as its first argument: list.add(i,s); //this inserts s at index i stephen haokip Also follow Sandra's advice whenever it's possible.


It's in general not a good idea to modify a list while iterating over it. What's the problem now for you? This loop already adds the copies as you wanted to, or did you want something different?


Kevin next to original


Thanks any way I figure it out


Sandra Meyer Original list - rose, lips, keel Output list should be - rose, lips, lips, keel, keel Two copies of any string containing the letter 'l' Plis any advice