Access Elements From a LinkedList in Java

Exercise:

Write a Java Program to access elements from a LinkedList.

1. Access elements from a linkedlist

Click Here to View the Solution!
import java.util.LinkedList;

public class linklist{
    public static void main(String[] args) {
        LinkedList<String> languages = new LinkedList<>();
        languages.add("Python");
        languages.add("Java");
        languages.add("JavaScript");
        languages.add("PHP");
        System.out.println("LinkedList: " + languages);

        // get the element from the LinkedList
        String str = languages.get(1);
        System.out.print("Element at index 1: " + str);
    }
}
Click Here to View the Output!
LinkedList: [Python, Java, JavaScript, PHP]
Element at index 1: Java
Click Here to View the Explanation!
  • In this example, a string type LinkedList called languages is created firstly.
  • Elements are added to this LinkedList and then the list is printed on the screen.
  • get() method is used to retrieve a specific element from the list by passing the index of the element which is to be fetched.
  • The fetched element is stored in String variable called str and then printed on the screen.

2.Access using iterator() method

Click Here to View the Solution!
import java.util.LinkedList;
import java.util.Iterator;

public class  linklist {
    public static void main(String[] args) {
        LinkedList<String> animals= new LinkedList<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");
        animals.add("Fish");

        // Creating an object of Iterator
        Iterator<String> iterate = animals.iterator();
        System.out.print("LinkedList: ");

        while(iterate.hasNext()) {
            System.out.print(iterate.next());
            System.out.print(", ");
        }
    }
}
Click Here to View the Output!
LinkedList: Dog, Horse, Cat, Fish
Click Here to View the Explanation!
  • In this example, a string type LinkedList called animals is created firstly.
  • Elements are added to this LinkedList.
  • Then an instance of Iterator called iterate is created.
  • A while loop is then executed which first checks if the next element exists in the LinkedList using hasNext() method.
  • If the condition is true, then the element in the LinkedList is printed using next() method.
  • This loop executes till it reaches the end of the LinkedList and so all elements of the list are printed.

3.Access using ListIterator() method

Click Here to View the Solution!
import java.util.LinkedList;
import java.util.ListIterator;

public class linklist{
    public static void main(String[] args) {
        LinkedList<String> animals= new LinkedList<>();
        animals.add("Dog");
        animals.add("Horse");
        animals.add("Cat");
        animals.add("Fish");

        // Create an object of ListIterator
        ListIterator<String> listIterate = animals.listIterator();
        System.out.print("LinkedList: ");
        while(listIterate.hasNext()) {
            System.out.print(listIterate.next());
            System.out.print(", ");
        }

        // Iterate backward
        System.out.print("\nReverse LinkedList: ");
        while(listIterate.hasPrevious()) {
            System.out.print(listIterate.previous());
            System.out.print(", ");
        }
    }
}
Click Here to View the Output!
LinkedList: Dog, Horse, Cat, Fish
Reverse LinkedList: Fish, Cat, Horse, Dog,
Click Here to View the Explanation!
  • In this example, a string type LinkedList called animals is created firstly.
  • Elements are added to this LinkedList.
  • Then an instance of ListIterator called listIterate is created.
  • A while loop is then executed which first checks if the next element exists in the LinkedList using hasNext() method.
  • If the condition is true, then the element in the LinkedList is printed using next() method.
  • The same process happens for accessing previous elements.
  • A while loop runs which first checks if the previous element exists in the LinkedList using hasPrevious() method.
  • If the condition is true, then the element in the LinkedList is printed using previous() method.
  • Both of these while loops execute till they reach the ends of the LinkedList and so all elements of the list are printed.