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.
-
What don't you understand about that method exactly?Mat– Mat2012-04-21 14:08:24 +00:00Commented Apr 21, 2012 at 14:08
-
i dont understand how to use itmadU– madU2012-04-21 14:23:35 +00:00Commented Apr 21, 2012 at 14:23
-
Object x = myarray.get(1);john k– john k2018-02-08 21:21:01 +00:00Commented Feb 8, 2018 at 21:21
6 Answers
Here is the official documentation of ArrayList.get().
Anyway it is very simple, for example
ArrayList list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
String str = (String) list.get(0); // here you get "1" in str
1 Comment
To put it nice and simply, get(int index) returns the element at the specified index.
So say we had an ArrayList of Strings:
List<String> names = new ArrayList<String>();
names.add("Arthur Dent");
names.add("Marvin");
names.add("Trillian");
names.add("Ford Prefect");
Which can be visualised as:
Where 0, 1, 2, and 3 denote the indexes of the ArrayList.
Say we wanted to retrieve one of the names we would do the following:
String name = names.get(1);
Which returns the name at the index of 1.
So if we were to print out the name System.out.println(name); the output would be Marvin - Although he might not be too happy with us disturbing him.
Comments
You use List#get(int index) to get an object with the index index in the list. You use it like that:
List<ExampleClass> list = new ArrayList<ExampleClass>();
list.add(new ExampleClass());
list.add(new ExampleClass());
list.add(new ExampleClass());
ExampleClass exampleObj = list.get(2); // will get the 3rd element in the list (index 2);
2 Comments
ArrayList get(int index) method is used for fetching an element from the list. We need to specify the index while calling get method and it returns the value present at the specified index.
public Element get(int index)
Example : In below example we are getting few elements of an arraylist by using get method.
package beginnersbook.com;
import java.util.ArrayList;
public class GetMethodExample {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("pen");
al.add("pencil");
al.add("ink");
al.add("notebook");
al.add("book");
al.add("books");
al.add("paper");
al.add("white board");
System.out.println("First element of the ArrayList: "+al.get(0));
System.out.println("Third element of the ArrayList: "+al.get(2));
System.out.println("Sixth element of the ArrayList: "+al.get(5));
System.out.println("Fourth element of the ArrayList: "+al.get(3));
}
}
Output:
First element of the ArrayList: pen
Third element of the ArrayList: ink
Sixth element of the ArrayList: books
Fourth element of the ArrayList: notebook
1 Comment
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