-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBox.java
More file actions
52 lines (40 loc) · 1.18 KB
/
Box.java
File metadata and controls
52 lines (40 loc) · 1.18 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
package GenericBox;
import java.util.ArrayList;
import java.util.List;
public class Box<T extends Comparable<T>> {
private T t;
public Box(T t) {
this.t = t;
}
private Class<T> checkClass(){
Class<T> o = (Class<T>) t.getClass();
return o;
}
@Override
public String toString() {
return String.format("%s: ", checkClass().getName()) + t;
}
public static <T> void swap(List<T> list,int firstIndex, int secondIndex){
T firstItem = null;
T secondItem = null;
for (int i = 0; i < list.size(); i++) {
if(i == firstIndex)
firstItem = list.get(i);
else if(i == secondIndex)
secondItem = list.get(i);
}
if(firstItem != null && secondItem != null){
list.set(firstIndex,secondItem);
list.set(secondIndex,firstItem);
}
}
public static <T> int countDoubles(List<? extends Comparable<T>> list, T o2){
int counter = 0;
for (Comparable<T> o1 : list) {
if(o1.compareTo(o2) > 0)
counter++;
}
return counter;
}
//{class full name: value}
}