EN
Java - get ArrayList item
0
points
In this article, we would like to show you how to get the ArrayList element in Java.
Quick solution:
myArrayList.get(index);
Where:
myArrayList
- ArrayList name,index
- index of the element to get access to.
Practical example
In this example, we use get()
method to get the element with index 1
from the fruits
ArrayList.
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
List<String> fruits = new ArrayList<>();
// add elements to the ArrayList
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
System.out.println(fruits.get(1)); // get element with index 1
}
}
Output:
Banana