Java LinkedList get() Method with Examples

In the previous article, we have discussed about Java LinkedList removeFirstOccurrence() Method with Examples

In this article we are going to see the use of Java LinkedList get() method along with suitable examples.

Java LinkedList get() Method with Examples

This java.util.LinkedList.get() method is used retrieve the element present in specified index position in a LinkedList.

It returns the element, which is present in the specified position of the LinkedList. If the index is out of the size/range of the LinkedList then it shows IndexOutOfBoundException.

Syntax:

LinkedListName.get(int indexPosition)

Where,

  • LinkedListName refers to the name of your LinkedList.
  • indexPosition refers to a particular index of LinkedList which element you want to get.

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList get() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now get the element from a specific position in the LinkedList using get( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("Hello");
        l.add("this");
        l.add("is");
        l.add("an");
        l.add("example of get() method");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // get the element from LinkedList using specific position
       System.out.println("Element at index 1: " + l.get(1));
    }
}
Output:

The elements of LinkedList are: [Hello, this, is, an, example of get() method]
Element at index 1: this

Example-2: Java LinkedList get() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Now get the element from a specific position in the LinkedList using get( ) method.
  • Print the element.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
        // get the element from LinkedList using specific position
       System.out.println("Element at index 1: " + l.get(2));
    }
}
Output:

The elements of LinkedList are: [2, 52, 13, 17, 1]
Element at index 1: 13

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: