Open In App
Related Articles

AbstractList get() method in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

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 list.

Exception: This method throws IndexOutOfBoundsException if the index is out of range (index = size()).

Below are the examples to illustrate the get() method.

Example 1:




// Java program to demonstrate
// get() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the value at the index 3
            // using get() method
            int value = arrlist1.get(3);
  
            // print the value
            System.out.println("Element at index 3 : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

ArrayListlist : [10, 20, 30, 40, 50]
Element at index 3 : 40

Example 2:




// Java program to demonstrate
// get() method
// for IndexOutOfBoundsException
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the value at the index 7
            // using get() method
            System.out.println("\nTrying to get "
                               + "the element from out"
                               + " of range index ");
            int value = arrlist1.get(7);
  
            // print the value
            System.out.println("Element at index 7 : "
                               + value);
        }
  
        catch (IndexOutOfBoundsException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

ArrayListlist : [10, 20, 30, 40, 50]

Trying to get the element from out of range index 
Exception thrown : java.lang.IndexOutOfBoundsException: Index: 7, Size: 5

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Nov, 2018
Like Article
Save Article
Similar Reads
Related Tutorials