I'm new to java (& to OOP too) and I'm trying to understand about the class ArrayList but I don't understand how to use the get(). I tried searching in net, but couldn't find anything helpful.
get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.
An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.
indexOf() in Java. The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
The get() method of List interface in Java is used to get the element present in this list at a given specific index. Syntax : E get(int index) Where, E is the type of element maintained by this List container.
The get() method returns an element. For example:
ArrayList<String> name = new ArrayList<String>();
name.add("katy");
name.add("chloe");
System.out.println("The first name in the list is " + name.get(0));
System.out.println("The second name in the list is " + name.get(1));
The output:
The first name in the list is katy The second name in the list is chloe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With