Java Program to Get Elements of a LinkedList
Last Updated :
27 Nov, 2020
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));
}
}
}
OutputLinkedList 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;
}
}
}
OutputThe 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;
}
}
}
OutputThe 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
Similar Reads
Java Program to Implement LinkedList API Linked List is a part of the Collection framework That is present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure in which the elements are not stored in contiguous locations and every element is a separate object with a data pa
10 min read
Java Program To Delete Middle Of Linked List Given a singly linked list, delete the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the linked list should be modified to 1->2->4->5 If there are even nodes, then there would be two middle nodes, we need to delete the second middle element. For example, if g
4 min read
Java Program to Get the First and the Last Element of a Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The given task is to retrieve the first and the last element of a given linked list. Properties of a Linked ListElements are stored in a non-contiguous manner.Every element is an object whi
4 min read
Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre
5 min read
Java Program To Delete Alternate Nodes Of A Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
3 min read
Java Program to Get Elements By Index from LinkedHashSet LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde
4 min read