Java program to add elements to a linkedlist
www.igaeditfi.com
Here's an example Java program that demonstrates how to add elements to a LinkedList
:
import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args) { LinkedList<String> list = new LinkedList<String>(); // add elements to the linked list list.add("Java"); list.add("Python"); list.add("C++"); list.add("JavaScript"); list.add("PHP"); // print the linked list System.out.println("Linked List: " + list); } }
In this example, we create a new LinkedList
object called list
. We then add five elements to the linked list using the add()
method. Finally, we print the contents of the linked list to the console using the System.out.println()
method.
The output of this program will be:
Linked List: [Java, Python, C++, JavaScript, PHP]