-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListDemo.java
More file actions
45 lines (40 loc) · 1.15 KB
/
LinkedListDemo.java
File metadata and controls
45 lines (40 loc) · 1.15 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
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class LinkedListDemo {
public static void main(String[] args) {
List<String> a = new LinkedList<>();
a.add("Amy");
a.add("Carl");
a.add("Erica");
List<String> b = new LinkedList<>();
b.add("Bob");
b.add("Doug");
b.add("Frances");
b.add("Gloria");
//merage the words from b into a
ListIterator<String> aIter = a.listIterator();
Iterator<String> bIter = b.iterator();
while (bIter.hasNext()) {
if (aIter.hasNext()) {
aIter.next();
}
aIter.add(bIter.next());
}
System.out.println(a);
//remove every seclond word from b
bIter = b.iterator();
while (bIter.hasNext()) {
bIter.next();
if (bIter.hasNext()) {
bIter.next();
bIter.remove();
}
}
System.out.println(b);
//bulk operation:remove all words in b from a
a.removeAll(b);
System.out.println(a);
}
}