-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListExamples.java
More file actions
57 lines (48 loc) · 1.97 KB
/
ListExamples.java
File metadata and controls
57 lines (48 loc) · 1.97 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.ArrayList;
import java.util.LinkedList;
/**
* This class demonstrates the use of ArrayList and LinkedList in Java.
* It includes examples of adding, accessing, and removing elements from both lists.
*/
public class ListExamples {
public static void main(String[] args) {
// Example of using ArrayList
System.out.println("ArrayList Example:");
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Apple");
arrayList.add("Banana");
arrayList.add("Cherry");
// Displaying the ArrayList
System.out.println("ArrayList: " + arrayList);
// Accessing elements
String firstElement = arrayList.get(0);
System.out.println("First element: " + firstElement);
// Removing an element
arrayList.remove("Banana");
System.out.println("ArrayList after removal: " + arrayList);
// Example of using LinkedList
System.out.println("\nLinkedList Example:");
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Dog");
linkedList.add("Cat");
linkedList.add("Rabbit");
// Displaying the LinkedList
System.out.println("LinkedList: " + linkedList);
// Accessing elements
String firstLinkedElement = linkedList.get(0);
System.out.println("First element in LinkedList: " + firstLinkedElement);
// Removing an element
linkedList.remove("Cat");
System.out.println("LinkedList after removal: " + linkedList);
// Iterating over the ArrayList
System.out.println("\nIterating over ArrayList:");
for (String fruit : arrayList) {
System.out.println(fruit);
}
// Iterating over the LinkedList
System.out.println("\nIterating over LinkedList:");
for (String animal : linkedList) {
System.out.println(animal);
}
}
}