Get specific ArrayList item
To get a specific item from an ArrayList in Java, you can use the get() method of the ArrayList class. The get() method returns the element at the specified position in the list.
Here is an example of how you can use the get() method to get an item from an ArrayList:
List<String> list = new ArrayList<>();
list.add("item1");
list.add("item2");
list.add("item3");
String item = list.get(1); // item is "item2"
Keep in mind that the get() method throws an IndexOutOfBoundsException if the index is out of range (index < 0 || index >= size()). You should make sure to check the size of the list before trying to access an element, or use the getOrDefault() method to specify a default value to be returned if the index is out of range.
For example:
List<String> list = new ArrayList<>();
list.add("item1");
list.add("item2");
list.add("item3");
int index = 1;
if (index >= 0 && index < list.size()) {
String item = list.get(index); // item is "item2"
} else {
// handle the error
}
List<String> list = new ArrayList<>();
list.add("item1");
list.add("item2");
list.add