Random data out of a list.
Say for example i create an array in the form,
how can I get an element of the list randomly for use, and then remove it from the list.
Code :
//Array List...
ArrayList<Integer> list = new ArrayList<Integer>();
//Data inside the list are the numbers from 0-9
for(int i = 0; i < 10; i++)
list.add(i);
Re: Random data out of a list.
Just get a random number and remove the element at that index, as follows:
Code java:
int rand = new Random().nextInt(list.size());
list.remove(rand);
Another alternative would be to use Collections.shuffle(List<?> list) to shuffle your ArrayList and pick the first few from the ArrayList.