Junior — Middle
What does the presence of the get() method in the ArrayList collection in Java provide?
sobes.tech AI
Answer from AI
The get(int index) method in the ArrayList collection in Java provides access to an element by its index. This means you can quickly retrieve the element located at a specific position in the list. Thanks to the internal structure of ArrayList (an array), the get() operation is performed in O(1) time, which is very fast.
Example of usage:
ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
String element = list.get(1); // Returns "Second"
Thus, having the get() method allows for efficient reading of elements by index, which is convenient for working with ordered data.