-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSection_6_5_LinkedList.java
More file actions
84 lines (58 loc) · 2.53 KB
/
Section_6_5_LinkedList.java
File metadata and controls
84 lines (58 loc) · 2.53 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
NOTES:
The 'LinkedList' class implements the 'List' interface, therefore it got all
'List' interface methods
To iterate a 'LinkedList' instance we have call 'ListIterator' method instead
'Iterator'
* ArrayList vs LinkedList
The ArrayList may be appropriate if insertions are performed only at the high
end of the array, The ArrayList doubles the internal array capacity if an
insertion at the high end would exceed the internal capacity, Although this
gives good Big-Oh performance, especially if we add a constructor that allows
the caller to suggest initial capacity for the internal array, the ArrayList is
a poor choice if insertions are not made at the end, because then we must move
items out of the way.
*/
import java.util.LinkedList;
import java.util.ListIterator;
public class Section_6_5_LinkedList {
public static void main(String[] arguments){
LinkedList<String> people = new LinkedList();
people.add("James");
people.add("Brendan");
people.add("George");
people.add("Terence");
people.add("Nikcy");
people.add("Thomas");
// Print LinkedList elements using ListIterator.
ListIterator<String> peopleIterator = people.listIterator();
while(peopleIterator.hasNext()){
System.out.println(peopleIterator.next());
}
System.out.println(" -------------------------------------------- ");
LinkedList<String> countries = new LinkedList();
countries.add("España");
countries.add("Colombia");
countries.add("Inglaterra");
countries.add("Mexico");
countries.add("Argentina");
LinkedList<String> capitals = new LinkedList();
capitals.add("Madrid");
capitals.add("Bogota D.C.");
capitals.add("Londres");
capitals.add("Ciudad de Mexico");
capitals.add("Buenon Aires");
System.out.println(countries);
System.out.println(capitals);
System.out.println(" -------------------------------------------- ");
ListIterator<String> countriesIterator = countries.listIterator();
ListIterator<String> capitalsIterator = capitals.listIterator();
while(capitalsIterator.hasNext()){
if(countriesIterator.hasNext()){
countriesIterator.next();
}
countriesIterator.add(capitalsIterator.next());
}
System.out.println(countries);
}
}