-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrial.java
More file actions
34 lines (28 loc) · 815 Bytes
/
Copy pathTrial.java
File metadata and controls
34 lines (28 loc) · 815 Bytes
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
import java.util.*;
class CountedInteger {
private static long counter;
private final long id = counter++;
public String toString() {
return Long.toString(id);
}
}
class FilledList<T> {
private Class<T> type;
public FilledList(Class<T> type) {
this.type = type;
}
public List<T> create(int nElements) {
List<T> result = new ArrayList<T>();
try {
for (int i = 0; i < nElements; i++)
result.add(type.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}
return result;
}
public static void main(String[] args) {
FilledList<CountedInteger> fl = new FilledList<CountedInteger>(CountedInteger.class);
System.out.println(fl.create(15));
}
}