Implement a set class with the following method: get(), put(), getRandom()
Anonymous
import java.util.ArrayList; public class S { private ArrayList values = new ArrayList(); private int getRandom() { int randomPos = (int) (System.currentTimeMillis() % values.size()); return values.get(randomPos); } public void put(int value) { values.add(value); } public int get(int pos) { return values.get(pos); } public static void main(String[] args) { S s = new S(); for (int i = 0; i < 10; i++) { s.put(i); System.out.println(s.getRandom()); } } }
Check out your Company Bowl for anonymous work chats.