How do you use ArrayList with a simple code example?

ArrayList is a resizable array that can hold elements and allows for dynamic array manipulation. It is a part of the Java Collections Framework and provides various methods to manipulate the array.

ArrayList, Java, Collections Framework, Resizable Array, Dynamic Array Manipulation
This example demonstrates how to use ArrayList in Java for basic operations like adding, removing, and iterating over elements.
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { // Create an ArrayList ArrayList fruits = new ArrayList<>(); // Add elements to the ArrayList fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); // Remove an element from the ArrayList fruits.remove("Banana"); // Iterate over the ArrayList for (String fruit : fruits) { System.out.println(fruit); } } }

ArrayList Java Collections Framework Resizable Array Dynamic Array Manipulation