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

  1. ArrayList<String> list = new ArrayList<>();
  2. Use add() to insert elements
  3. For-each loop to iterate

Related Collections exercises

Practice all Collections exercises · Run this idea in the Java compiler