Java LinkedList - add() Method
The java.util.LinkedList.add() method is used to append the specified element at the end of the list.
Syntax
public boolean add(E element)
Here, E is the type of element maintained by the container.
Parameters
element |
Specify element which need to be appended in the list. |
Return Value
Returns true if the element is successfully appended, else returns false.
Exception
NA
Example:
In the example below, the java.util.LinkedList.add() method is used to append the specified element at the end of the list.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a linkedlist LinkedList<Integer> MyList = new LinkedList<Integer>(); //populating linkedlist using add method MyList.add(10); MyList.add(20); MyList.add(30); //printing linkedlist System.out.println("MyList contains: " + MyList); } }
The output of the above code will be:
MyList contains: [10, 20, 30]
❮ Java.util - LinkedList