3. Create an ArrayList of strings to store the names of celebrities or athletes. Add five names to the list. Process the list with a for loop and the get() method to display the names, one name per line. Pass the list to a void method. Inside the method, Insert another name at index 2 and remove the name at index 4. Use a foreach loop to display the arraylist again, all names on one line separated by asterisks. After the method call in main, create an iterator for the arraylist and use it to display the list one more time. See Sample Output. SAMPLE OUTPUT Here is the list Lionel Messi Drake Adele Dwayne Johnson Beyonce Here is the new list * Lionel Messi * Drake Taylor Swift * Adele * Beyonce Using an iterator, here is the list Lionel Messi Drake Taylor Swift Adele Beyonce
![3. Create an ArrayList of strings to store the names of celebrities or athletes. Add five names to the list. Process the list with a for loop and the get() method
to display the names, one name per line. Pass the list to avoid method. Inside the method, Insert another name at index 2 and remove the name at index 4.
Use a foreach loop to display the arraylist again, all names on one line separated by asterisks. After the method call in main, create an iterator for the
arraylist and use it to display the list one more time. See Sample Output.
SAMPLE OUTPUT
Here is the list
Lionel Messi
Drake
Adele
Dwayne Johnson
Beyonce
Here is the new list
* Lionel Messi * Drake * Taylor Swift * Adele * Beyonce
Using an iterator, here is the list
Lionel Messi
Drake
Taylor Swift
Adele
Beyonce](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fa8d3ff56-1af2-434e-8740-80c67d4b592b%2F3b01ba5b-67dd-495a-81bb-c9d9e478778f%2Fksmdqq5_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
The solution to the given question is:
import java.util.ArrayList;
import java.util.Iterator;
public class PlaywithList {
public static void ModifyList(ArrayList<String> list) {
list.add(2, "Taylor Swift ");
list.remove(4);
System.out.println("Here is the new list");
for (String str : list)
System.out.print("*"+str);
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<String> list = new ArrayList<Strings>();
list.add("Lionel Messi");
list.add("Drake");
list.add("Adele");
list.add("Dwayne Johnson");
list.add("Beyonce");
System.out.println("Here is the list");
for (int i = 0; i < list.size(); ++i)
System.out.println(list.get(i));
ModifyList(list);
System.out.println("Using an iterator, here is the list");
Iterator<String> it = list.iterator();
while (it.hasNext())
System.out.println(it.next));
}
}
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)