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
import java.io.*;
import java.util.LinkedList;
class GFG {
public static void main(String[] args)
{
LinkedList<String> gfg = new LinkedList<String>();
gfg.add( "GEEKS" );
gfg.add( "FOR" );
gfg.add( "GEEKS" );
System.out.println( "LinkedList Elements : " );
for ( int i = 0 ; i < gfg.size(); 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
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>();
gfg.add( "GEEKS" );
gfg.add( "FOR" );
gfg.add( "GEEKS" );
Iterator<String> i = gfg.iterator();
System.out.print(
"The elements of the input LinkedList: \n" );
int j = 0 ;
while (i.hasNext()) {
System.out.print( "The element at the index " + j
+ " " );
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
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>();
gfg.add( "GEEKS" );
gfg.add( "FOR" );
gfg.add( "GEEKS" );
ListIterator<String> li = gfg.listIterator();
System.out.print(
"The elements of the LinkedList: \n" );
int j = 0 ;
while (li.hasNext()) {
System.out.print( "The element at the index " + j
+ " " );
System.out.print(li.next());
System.out.print( "\n" );
++j;
}
--j;
System.out.print(
"\nThe elements of the LinkedList in Reverse order: \n" );
while (li.hasPrevious()) {
System.out.print( "The element at the index " + j
+ " " );
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
Share your thoughts in the comments
Please Login to comment...