Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaJava.util.LinkedList.get(), getFirst(), getLast() in Java

Java.util.LinkedList.get(), getFirst(), getLast() in Java

In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList:

  1. get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first element has an index of 0.
  2. getFirst(): This method returns the first element in the LinkedList. If the LinkedList is empty, a NoSuchElementException is thrown.
  3. getLast(): This method returns the last element in the LinkedList. If the LinkedList is empty, a NoSuchElementException is thrown.

Here’s an example code that demonstrates the use of these methods:

Java




// Java code to demonstrate the working
// of get(int index) in linked list
 
import java.util.*;
public class LinkedListget1 {
   
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
 
        // printing the whole list
        System.out.println("The elements in List are : "
                           + list);
 
        // using get() to print element at index 3
        // prints 8
        System.out.println("Element at index 3 is : "
                           + list.get(3));
    }
}


Output

The element at index 2 is: Charlie
The first element of the LinkedList is: Alice
The last element of the LinkedList is: Dave

In this code, we first create a LinkedList of strings, and then add four elements to it using the add() method.

We then use the get(int index) method to get the element at index 2 (which is “Charlie”), and print it to the console using the println() method.

Next, we use the getFirst() method to get the first element of the LinkedList (which is “Alice”), and print it to the console using the println() method.

Finally, we use the getLast() method to get the last element of the LinkedList (which is “Dave”), and print it to the console using the println() method.

The conventional method allowing to get the element at particular index is get(). Though in LinkedList its never possible to achieve this without complete traversal, but this method allows the same. Three variants present, all of which are discussed in this article with Exceptions as well. 

1. get(int index) : This method returns the element at the specified position in this list.

Declaration : 
    public E get(int index)
Parameters : 
    index :  index of the element to return
Return Value : 
   This method returns the element at the specified position in this list
Exception
     IndexOutOfBoundsException : if the index is out of range

Java




// Java code to demonstrate the working
// of getFirst() in linked list
 
import java.util.*;
public class LinkedListget2 {
   
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
 
        // printing the whole list
        System.out.println("The elements in List are : "
                           + list);
 
        // using get() to print element at first index
        // prints "Geeks"
        System.out.println("Element at 1st index is : "
                           + list.getFirst());
    }
}


Output

The elements in List are : [Geeks, 4, Geeks, 8]
Element at 1st index is : Geeks

2. getFirst() : This method returns the first element in this list.

Declaration : 
    public E getFirst()
Return Value : 
    This method returns the first element in this list
Exceptions : 
     NoSuchElementException : if this list is empty

Java




// Java code to demonstrate the working
// of getFirst() in linked list
import java.util.*;
public class LinkedListget2 {
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
 
        // printing the whole list
        System.out.println("The elements in List are : "
                           + list);
 
        // using get() to print element at first index
        // prints "Geeks"
        System.out.println("Element at 1st index is : "
                           + list.getFirst());
    }
}


Output :

The elements in List are : [Geeks, 4, Geeks, 8]
Element at 1st index is : Geeks

3. getLast() : This method returns the last element in this list.

Declaration : 
    public E getLast()
Return Value : 
   This method returns the last element in this list
Exceptions : 
   NoSuchElementException : if this list is empty

Java




// Java code to demonstrate the working
// of getLast() in linked list
import java.util.*;
public class LinkedListget3 {
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
 
        // printing the whole list
        System.out.println("The elements in List are : "
                           + list);
 
        // using get() to print element at last index
        // prints "8"
        System.out.println("Element at last index is : "
                           + list.getLast());
    }
}


Output:

The elements in List are : [Geeks, 4, Geeks, 8]
Element at last index is : 8

 

Exceptions

1. IndexOutOfBoundException 

Java




// Java code to demonstrate the Exceptions
// of get()
import java.util.*;
public class LinkedListExcep1 {
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // adding elements using add()
        list.add("Geeks");
        list.add("4");
        list.add("Geeks");
        list.add("8");
 
        // Trying to get element at index 7
        // throws exception
        System.out.println("The element at index 7 is : "
                           + list.get(7));
    }
}


Output :

No Output

Runtime Error :

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 4
    at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
    at java.util.LinkedList.get(LinkedList.java:476)
    at LinkedListExcep1.main(LinkedListExcep1.java:22)

2. NoSuchElementException 

Java




// Java code to demonstrate the Exceptions
// of getFirst()
import java.util.*;
public class LinkedListExcep2 {
    public static void main(String[] args)
    {
        // declaring a LinkedList
        LinkedList<String> list = new LinkedList<String>();
 
        // Trying to get first element at index 7
        // throws exception
        System.out.println("The first element of list is : "
                           + list.getFirst());
    }
}


Output :

No Output

Runtime Error :

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getFirst(LinkedList.java:244)
    at LinkedListExcep2.main(LinkedListExcep2.java:15)

This article is contributed by Astha Tyagi. If you like Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments