-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListExample.java
32 lines (25 loc) · 999 Bytes
/
LinkedListExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> names = new LinkedList<>();
// Adding elements to the LinkedList
names.add("John");
names.add("Alice");
names.add("Bob");
// Accessing elements using index
System.out.println("Element at index 1: " + names.get(1));
// Iterating over the LinkedList
System.out.println("Elements in the LinkedList:");
for (String name : names) {
System.out.println(name);
}
// Checking if an element exists
boolean containsAlice = names.contains("Alice");
System.out.println("LinkedList contains Alice: " + containsAlice);
// Removing an element
names.remove("Bob");
// Size of the LinkedList
System.out.println("Size of the LinkedList: " + names.size());
}
}