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

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

6/20/2020 4:09:56 AM

stephen haokip

6 Answers

New Answer

+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?

+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.

+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?

+1

Kevin next to original

+1

Thanks any way I figure it out

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