Get LinkedList elements

We can use get() method for retrieving the element from LinkedList

There are 3 different get() methods in LinkedList


1) Object get(int index);
2) Object getFirst();
3) Object getLast();

Let’s discuss these methods with example

Object get(int index)

It returns the value present at the specified index
This method throws IndexOutOfBoundsException if the index is less than zero or greater than the size of the list

Example :

  1. import java.util.*;
  2. public class LinkedListGetFromIndex {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("First element of the LinkedList: "+list.get(0));
  10. System.out.println("Third element of the LinkedList: "+list.get(2));
  11.     }
  12. }
import java.util.*;
public class LinkedListGetFromIndex {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("First element of the LinkedList: "+list.get(0));
System.out.println("Third element of the LinkedList: "+list.get(2));
	}
}


Object getFirst()

This method returns the first element from the list

Example

  1. import java.util.*;
  2. public class LinkedListGetFirst {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("First element of the LinkedList: "+list.getFirst());
  10.  
  11.     }
  12. }
import java.util.*;
public class LinkedListGetFirst {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("First element of the LinkedList: "+list.getFirst());

	}
}


Object getLast()

This method returns the last element from the list

Example

  1. import java.util.*;
  2. public class LinkedListGetLast {
  3.     public static void main(String[] args) {
  4.     LinkedList<String> list = new LinkedList<String>();
  5.     list.add("java");
  6. list.add("c++");
  7. list.add("python");
  8. list.add("c");
  9. System.out.println("Last element of the LinkedList: "+list.getLast());
  10.  
  11.     }
  12. }
import java.util.*;
public class LinkedListGetLast {
	public static void main(String[] args) {
	LinkedList<String> list = new LinkedList<String>();
	list.add("java");
list.add("c++");
list.add("python");
list.add("c");
System.out.println("Last element of the LinkedList: "+list.getLast());

	}
}

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on