-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRandomList.java
More file actions
35 lines (26 loc) · 840 Bytes
/
RandomList.java
File metadata and controls
35 lines (26 loc) · 840 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
35
package generics;
import java.util.*;
/**
* RUN:
* javac generics/RandomList.java && java generics.RandomList
* OUTPUT:
* brown over fox quick quick dog brown The brown lazy brown
*/
public class RandomList<T> {
private ArrayList<T> storage = new ArrayList<T>();
private Random rand = new Random(47);
public void add(T item) { storage.add(item); }
public T select() {
return storage.get(rand.nextInt(storage.size()));
}
public static void main(String[] args) {
RandomList<String> rs = new RandomList<String>();
for (String s : "The quick brown fox jumped over the lazy brown dog".split(" ")) {
rs.add(s);
}
for (int i = 0; i < 11; i++) {
System.out.print(rs.select() + " ");
}
System.out.println();
}
}