-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLessonThirteen.java
More file actions
152 lines (116 loc) · 4.29 KB
/
LessonThirteen.java
File metadata and controls
152 lines (116 loc) · 4.29 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package newLessons.lessonThirteen;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import newLessons.Lesson;
public class LessonThirteen extends Lesson {
@Override
public void theLesson() {
List<Integer> arrayList = new ArrayList<>();
List<Integer> linkedList = new LinkedList<>();
Random r = new Random();
for (int x = 0; x < r.nextInt(45); x++) {
arrayList.add(r.nextInt(50));
linkedList.add(r.nextInt(50));
}
for (int i : arrayList) {
System.out.println(i);
}
System.out.println("--------------");
Iterator<Integer> it = linkedList.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("--------------");
arrayList.clear();
linkedList.clear();
Collections.addAll(arrayList, 1, 6, 5, 8);
Collections.addAll(linkedList, 1, 2, 3, 4);
Collections.copy(linkedList, arrayList);
System.out.println(arrayList);
System.out.println(linkedList);
// Remember that even though they are in List<Integer> variables I could have easily passed in ArrayList<Integer> or LinkedList<Integer>
// variables into the printAll method as they would just be cast down as I did when I stored them in the varaible.
// The point of storing it in the variable was just to show you good practice and the fact that ArrayList and LinkedList are just implementations.
// They really don't add much more.
Collections.fill(linkedList, 8);
System.out.println(arrayList);
System.out.println(linkedList);
Collections.shuffle(arrayList);
System.out.println(arrayList);
Collections.sort(arrayList);
System.out.println(arrayList);
linkedList.clear();
for (int x = 0; x < r.nextInt(45); x++) {
linkedList.add(r.nextInt(50));
}
// Here I am showing you that this weird comparator follows the contract even though that is not necessary.
// I know it follows the first part about 1 < 100 and 100 > 1 but the transitive part (x < y < z) I am not sure about (due to that whole even thing)
// That means this may not be a proper comparator although it looks like it is fine.
Comparator<Integer> w = new Weirdo();
System.err.println(w.compare(1, 100)); // -
System.err.println(w.compare(100, 1)); // +
System.err.println(w.compare(1, 100)); // -
System.err.println(w.compare(100, 106));// -
System.err.println(w.compare(1, 106)); // -
// I could have used the old one above but I usually just create my comparators on the fly unless they are needed often.
Collections.sort(linkedList, new Weirdo());
System.out.println(linkedList);
}
/*
* As you can see here, when implementing or extending a class with generics you have to include the generics.
* You can either explicitly state it like I do in our comparator below (at it will only be used for Integers).
* Or you can have the class take a generic type and then pass that generic type to the parent class as is done in the example.
* I usually explicitly state it for my Comparators so that way it can only be used on the type I want and will work as expected.
* Although certain cases call for different things.
*/
private class Weirdo implements Comparator<Integer> {
// This comparator will put even numbers above odd numbers and then bigger numbers above smaller numbers.
@Override
public int compare(Integer o1, Integer o2) {
if (o1 == o2) {
return 0;
}
boolean e1 = o1 % 2 == 0;
boolean e2 = o2 % 2 == 0;
if (e1 && e2 || !e1 && !e2) {
return o1 > o2 ? 1 : -1;
} else {
return e1 ? 1 : -1;
}
}
}
private class Example<T> implements Comparator<T> {
@Override
public int compare(T o1, T o2) {
// This class is useless.
return 0;
}
}
@Override
public void theAssignment() {
List<Entity> l = new LinkedList<>();
Random r = new Random();
for (int x = 0; x < r.nextInt(20) + 8; x++) {
// Add is O(1)
l.add(new Entity());
}
System.out.println("Not done!");
while (!l.isEmpty()) {
Iterator<Entity> i = l.iterator();
while (i.hasNext()) {
Entity e = i.next();
e.update();
if (e.isDead) {
// Removing through the use of an iterator is O(1) (normally removing is not for the linked list!).
i.remove();
}
}
}
System.out.println("Done!");
}
}