java arraylist

java

In Java, the ArrayList class is a resizable array implementation of the List interface. It provides a flexible and efficient way to store and manipulate a sequence of objects.

The ArrayList class is part of the java.util package and can be used to store a collection of objects of any type. It allows you to add, remove, and access elements dynamically and automatically resizes the underlying array as needed.

Here is an example of how to create and use an ArrayList in Java:

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    // Create an ArrayList of strings
    ArrayList<String> names = new ArrayList<>();

    // Add elements to the ArrayList
    names.add("John");
    names.add("Jane");
    names.add("James");

    // Access elements in the ArrayList
    System.out.println(names.get(0)); // prints "John"
    System.out.println(names.get(1)); // prints "Jane"
    System.out.println(names.get(2)); // prints "James"

    // Remove an element from the ArrayList
    names.remove(1);

    // Iterate over the ArrayList
    for (String name : names) {
      System.out.println(name);
    }
  }
}

This code creates an ArrayList of strings and adds three elements to it. It then accesses and removes an element from the ArrayList, and iterates over the ArrayList using a for-each loop.

For more information on the ArrayList class and how to use it in Java, you can refer to the Java documentation: