Open In App

Java Program to Get Elements of a LinkedList

Last Updated : 27 Nov, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Linked List is a linear data structure, in which the elements are not stored at the contiguous memory locations. Here, the task is to get the elements of a LinkedList.

1. We can use get(int variable) method to access an element from a specific index of LinkedList:

In the given example, we have used the get(i) method. Here, the method returns the element which is at the i th index.

Syntax:

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

Return Value: The method returns the element present at the position specified by the parameter index.

Java




// Java program to get the elements of Linkedlist
  
import java.io.*;
import java.util.LinkedList;
class GFG {
    public static void main(String[] args)
    {
  
        // Creating LinkedList
        LinkedList<String> gfg = new LinkedList<String>();
  
        // Adding values
        gfg.add("GEEKS");
        gfg.add("FOR");
        gfg.add("GEEKS");
  
        System.out.println("LinkedList Elements : ");
  
        for (int i = 0; i < gfg.size(); i++) {
  
            // get(i) returns element present at index i
            System.out.println("Element at index " + i
                               + " is: " + gfg.get(i));
        }
    }
}


Output

LinkedList Elements : 
Element at index 0 is: GEEKS
Element at index 1 is: FOR
Element at index 2 is: GEEKS

2. We can use the iterator() method

  • To use this method we have to import java.util.Iterator package.
  • In this method, we can iterate over the LinkedList and then extract the element at the given index accordingly.

Java




// Java program to iterate over linkedlist
// to extract elements of linkedlist
  
import java.io.*;
import java.util.LinkedList;
import java.util.Iterator;
class GFG {
    public static void main(String[] args)
    {
  
        LinkedList<String> gfg = new LinkedList<String>();
  
        // Adding elements
        gfg.add("GEEKS");
        gfg.add("FOR");
        gfg.add("GEEKS");
  
        // Create an object of Iterator
        Iterator<String> i = gfg.iterator();
  
        System.out.print(
            "The elements of the input LinkedList: \n");
  
        int j = 0;
  
        // has.next() returns true if there is a next
        // element
        while (i.hasNext()) {
  
            System.out.print("The element at the index " + j
                             + " ");
  
            // next() returns the next element
            String str = i.next();
  
            System.out.print(str);
            System.out.print(" \n");
  
            ++j;
        }
    }
}


Output

The elements of the input LinkedList: 
The element at the index 0 GEEKS 
The element at the index 1 FOR 
The element at the index 2 GEEKS

3. We can use ListIterator() method.

  • ListIterator() is a subinterface of Iterator() method.
  • It provides us with the function to access the elements of a list.
  • It is bidirectional that means it allows us to iterate elements of a list in the both the direction.
  • To use this method we have to import java.util.ListIterator.

Java




// Java program to iterate over the
// linkedlist using listIterator()
  
import java.io.*;
import java.util.LinkedList;
import java.util.ListIterator;
  
class GFG {
    public static void main(String[] args)
    {
  
        LinkedList<String> gfg = new LinkedList<String>();
  
        // Adding elements
        gfg.add("GEEKS");
        gfg.add("FOR");
        gfg.add("GEEKS");
  
        // Create an object of ListIterator
        ListIterator<String> li = gfg.listIterator();
  
        System.out.print(
            "The elements of the LinkedList: \n");
  
        // hasNext() returns true if there is next element
        int j = 0;
  
        while (li.hasNext()) {
  
            // next() returns the next element
            System.out.print("The element at the index " + j
                             + " ");
  
            System.out.print(li.next());
  
            System.out.print("\n");
  
            ++j;
        }
        --j;
  
        // Now to show that ListIterator() can traverse in
        // both the direction
        System.out.print(
            "\nThe elements of the LinkedList in Reverse order: \n");
  
        // hasprevious() checks if there is a previous
        // element
        while (li.hasPrevious()) {
  
            System.out.print("The element at the index " + j
                             + " ");
  
            // previous() returns the previous element
            System.out.print(li.previous());
            System.out.print("\n");
  
            --j;
        }
    }
}


Output

The elements of the LinkedList: 
The element at the index 0 GEEKS
The element at the index 1 FOR
The element at the index 2 GEEKS

The elements of the LinkedList in Reverse order: 
The element at the index 2 GEEKS
The element at the index 1 FOR
The element at the index 0 GEEKS


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads