[Java Snippet008]_List_001

TALK IS CHEAP SHOW ME THE CODE!

package chapter15._2._3;

import java.util.ArrayList;
import java.util.Random;

public class RandomList {

    private ArrayList storage = new ArrayList<>();
    private Random random = new Random(47);

    public void add(T item) {
        storage.add(item);
    }

    public T select() {
        return storage.get(random.nextInt(storage.size()));
    }

    public static void main(String[] args) {
        RandomList rs = new RandomList<>();
        for(String s : "The quick brown fox jumped over the lazy dog".split(" "))
            rs.add(s);
        for(int i = 0; i < 10; i++)
            System.out.print(rs.select() + " ");

    }

}

Output:

brown log jumped brown jumped brown quick over The jumped

你可能感兴趣的:([Java Snippet008]_List_001)