Adding an Element to a LinkedList
In the previous post, we have seen LinkedList in java.In this post, we will learn how to add element to a linkedList.
We use the method add() to add the elements to the LinkedList. It maintains the insertion order for the elements.
import java.util.LinkedList; public class LinkedListEx { public static void main(String[] args) { LinkedList<String> fruits1 = new LinkedList<String>(); fruits1.add("Orange"); fruits1.add("Mango"); fruits1.add("Apple"); fruits1.add("Grapes"); System.out.println("All fruits :"+ fruits1); } }
Output:
All fruits : [Orange, Mango, Apple, Grapes]
Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum