ArrayList Basics in Java: Explanation & Practice
Create and use ArrayList
Problem summary
Create an ArrayList, add 5 names, and print them all.
Starter code
import java.util.*;
public class Main {
public static void main(String[] args) {
// Create ArrayList of String
// Add: Alice, Bob, Carol, David, Eve
// Print each name
}
}Expected output and test cases
- Print all names
Alice Bob Carol David Eve
Hints
- ArrayList<String> list = new ArrayList<>();
- Use add() to insert elements
- For-each loop to iterate
Related Collections exercises
- Practice HashSet Basics in Java
- Practice HashMap Basics in Java
- Practice List Iteration Methods in Java
Practice all Collections exercises · Run this idea in the Java compiler