2015-04-20 15:36:01 -07:00
|
|
|
|
//: generics/RandomList.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class RandomList<T> {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
private ArrayList<T> storage = new ArrayList<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
|
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) {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
RandomList<String> rs = new RandomList<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
|
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() + " ");
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
brown over fox quick quick dog brown The brown lazy brown
|
|
|
|
|
*///:~
|