Java LinkedList - get() Method
The java.util.LinkedList.get() method returns the element at the specified index number in the list.
Syntax
public E get(int index)
Here, E is the type of element maintained by the container.
Parameters
index |
Specify the index number of the element which need to be returned from the list. |
Return Value
Returns the element of the list at the specified index number.
Exception
Throws IndexOutOfBoundsException, if the index is out of range (index < 0 || index >= size()).
Example:
In the example below, the java.util.LinkedList.get() method is used to display the content of the given list.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a linkedlist LinkedList<Integer> MyList = new LinkedList<Integer>(); //populating linkedlist MyList.add(10); MyList.add(20); MyList.add(30); MyList.add(40); //printing linkedlist System.out.print("MyList contains: "); for(int i = 0; i < MyList.size(); i++) { System.out.print(MyList.get(i)+ " "); } } }
The output of the above code will be:
MyList contains: 10 20 30 40
❮ Java.util - LinkedList