Junior — Middle
What is the main function of the ArrayList collection in Java?
sobes.tech AI
Answer from AI
The main function of the ArrayList collection in Java is to store elements in a dynamically resizable array with quick access by index. ArrayList allows adding, removing, and modifying elements, automatically increasing the size of the internal array when necessary. It is a convenient data structure when fast access to elements and their insertion order are important.
Example of usage:
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
System.out.println(list.get(0)); // Will output "First"
}
}