0

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. https://code.sololearn.com/cPX0Sm23dCa9/?ref=app

20th Jun 2020, 4:09 AM
stephen haokip
stephen haokip - avatar
6 ответов
+ 2
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?
20th Jun 2020, 4:55 AM
Kevin ★
+ 3
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.
20th Jun 2020, 5:09 AM
Kevin ★
+ 2
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?
20th Jun 2020, 4:59 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Kevin next to original
20th Jun 2020, 5:04 AM
stephen haokip
stephen haokip - avatar
+ 1
Thanks any way I figure it out
20th Jun 2020, 5:27 AM
stephen haokip
stephen haokip - avatar
0
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
20th Jun 2020, 5:17 AM
stephen haokip
stephen haokip - avatar