Final answer:
To create and use an ArrayList to store String objects, instantiate it, add Strings using the add() method, and then iterate through the list using a for-each loop to display the names.
Step-by-step explanation:
To create an ArrayList that holds String objects, add names to it, and display its contents, you can use the following Java code:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Create an ArrayList to hold String objects
ArrayList names = new ArrayList();
// Add three names to the ArrayList
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Display the contents of the ArrayList
System.out.println("Names in the ArrayList:");
for(String name : names) {
System.out.println(name);
}
}
}
In this code, we first import the ArrayList class and then declare it to hold objects of type String. We then instantiate the ArrayList and add three names to it using the add method. Finally, we use a for-each loop to iterate through the ArrayList and print each name to the console.