Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ArrayList's get() method

Tags:

java

arraylist

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.

like image 541
madU Avatar asked Apr 21 '12 14:04

madU


People also ask

What is get () method in Java?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array.

How do I get an element from an ArrayList?

An element can be retrieved from the ArrayList in Java by using the java. util. ArrayList. get() method.

Can we get index from value in ArrayList in Java?

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.

What does get in list do?

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.


1 Answers

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

like image 67
user15908054 Avatar answered Sep 24 '22 22:09

user15908054