Open In App

List get() method in Java with Examples

Last Updated : 11 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Parameter : This method accepts a single parameter index of type integer which represents the index of the element in this list which is to be returned.

Return Value: It returns the element at the specified index in the given list.

Errors and exception : This method throws an IndexOutOfBoundsException if the index is out of range (index=size()).

Below programs illustrate the get() method:

Program 1 :




// Java code to demonstrate the working of
// get() method in List
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating an Empty Integer List
        List<Integer> arr = new ArrayList<Integer>(4);
  
        // using add() to initialize values
        // [10, 20, 30, 40]
        arr.add(10);
        arr.add(20);
        arr.add(30);
        arr.add(40);
  
        System.out.println("List: " + arr);
  
        // element at index 2
        int element = arr.get(2);
  
        System.out.println("The element at index 2 is " + element);
    }
}


Output:

List: [10, 20, 30, 40]
The element at index 2 is 30

Program 2 : Program to demonstrate the error.




// Java code to demonstrate the error of
// get() method in List
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // creating an Empty Integer List
        List<Integer> arr = new ArrayList<Integer>(4);
  
        // using add() to initialize values
        // [10, 20, 30, 40]
        arr.add(10);
        arr.add(20);
        arr.add(30);
        arr.add(40);
  
        try {
            // Trying to access element at index 8
            // which will throw an Exception
            int element = arr.get(8);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}


Output:

java.lang.IndexOutOfBoundsException: Index: 8, Size: 4

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/List.html#get(int)



Previous Article
Next Article

Similar Reads

Get Credential Information From the URL(GET Method) in Java
Given a URL as a string, the task is to extract username, password, profile, role, and key from the URL in a GET Method. Examples: Input: http://www.geeksforgeeks.com/signin/service?username=test&amp;pwd=test&amp;profile=developer&amp;role=ELITE&amp;key=managerOutput:username: testpwd: testprofile: developerrole: ELITEkey: manager Input: http://www
1 min read
ArrayList get(index) Method in Java with Examples
The get() method of ArrayList in Java is used to get the element of a specified index within the list. Syntax: get(index) Parameter: Index of the elements to be returned. It is of data-type int. Return Type: The element at the specified index in the given list. Exception: It throws IndexOutOfBoundsException if the index is out of range (index=size(
2 min read
LinkedHashMap get() Method in Java with Examples
In Java, get() method of LinkedHashMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. --&gt; java.util Package --&gt; LinkedHashMap Class --&gt; get() Method Syntax: Linked_Hash_Map.get(Object key_element) Parameter: One parameter
2 min read
Year get() method in Java with Examples
get() method of the Year class used to get the value for the specified field passed as parameter from this Year as an integer value. This method queries this year for the value of the field and the returned value will always be within the valid range of values for the field. When the field is not supported and method is unable to return int value t
2 min read
ByteBuffer get() method in Java with Examples
get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflowException – If the buffer’s current position is no
6 min read
Provider get() method in Java with Examples
The get() method of java.security.Provider class is used to return the value to which the specified key is mapped, or null if this map contains no mapping for the key. More formally, if this map contains a mapping from a key k to a value v such that (key.equals(k)), then this method returns v; otherwise it returns null. (There can be at most one su
3 min read
AbstractList get() method in Java with Examples
The get() method of java.util.AbstractList class is used to return the element at the specified position in this list. Syntax: public abstract E get(int index) Parameters: This method takes index of the element as a parameter, the element at which is to be returned. Returns Value: This method returns the element at the specified position in this li
2 min read
AbstractMap get() Method in Java with Examples
The AbstractMap.get() method of AbstractMap class is used to retrieve or fetch the value mapped by a particular key mentioned in the parameter. It returns NULL when the map contains no such mapping for the key. Syntax: AbstractMap.get(Object key_element) Parameter: The method takes one parameter key_element of object type and refers to the key whos
2 min read
AbstractSequentialList get() method in Java with Examples
The get() method of AbstractSequentialList is used to fetch or retrieve an element at a specific index from a AbstractSequentialList. Syntax: AbstractSequentialList.get(int index) Parameters: The parameter index is of integer data type that specifies the position or index of the element to be fetched from the AbstractSequentialList. Return Value: T
2 min read
Duration get(TemporalUnit) method in Java with Examples
The get(TemporalUnit) method of Duration Class in java.time package is used to get the value of the unit passed as the parameter. Only units SECONDS and NANOS ate supported by this method and the rest result in exception. Syntax: public long get(TemporalUnit unit) Parameters: This method accepts a parameter unit which is the TemporalUnit for which
2 min read