ArrayList in Java is a resizable array implementation of the List interface. It provides dynamic arrays that can grow as needed, offering more flexibility than static arrays. Below are various examples demonstrating the use of ArrayList, including common operations like adding, removing, and iterating over elements. Basic Operations Creating an ArrayList
|
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> colors = new ArrayList<>(); colors.add("Red"); colors.add("Green"); colors.add("Blue"); System.out.println(colors); // Output: [Red, Green, Blue] } } |
Accessing Elements
|
String firstColor = colors.get(0); System.out.println("First color: " + firstColor); // Output: First color: Red |
…
Read more