- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Add a single element to a LinkedList in Java
A single element can be added to a LinkedList by using the java.util.LinkedList.add() method. This method has one parameter parameters i.e. the element that is to be inserted in the LinkedList.
A program that demonstrates this is given as follows −
Example
import java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList<String> l = new LinkedList<String>(); l.add("Magic"); System.out.println("The LinkedList is: " + l); } }
Output
The LinkedList is: [Magic]
Now let us understand the above program.
The LinkedList l is created. Then LinkedList.add() is used to add a single element to the LinkedList. Then the LinkedList is displayed. A code snippet which demonstrates this is as follows −
LinkedList<String> l = new LinkedList<String>(); l.add("Magic"); System.out.println("The LinkedList is: " + l);
- Related Articles
- Java Program to Get the middle element of LinkedList in a single iteration
- Java Program to Add elements to a LinkedList
- Search a particular element in a LinkedList in Java
- Golang Program to Get the middle element of LinkedList in a single iteration
- Remove a specific element from a LinkedList in Java
- Replace an element of a Java LinkedList
- Retrieve the last element from a LinkedList in Java
- Delete first and last element from a LinkedList in Java
- Golang program to add elements to a linkedlist
- How do you find the element of a LinkedList in Java?
- Add an element to a Stack in Java
- Remove single element from a HashSet in Java
- Java Program to Detect loop in a LinkedList
- How to remove an element from ArrayList or, LinkedList in Java?
- Add elements at beginning and end of LinkedList in Java

Advertisements