0

I have an arrayList where I have entered some strings (names). The assignment is pretty simple but I have no idea how to do it. We have to make a program that will print out a random String in the console from the list.

I've come this far:

ArrayList <String> p = new ArrayList <String> ();
Random ran = new Random ();

p.add("Petter");
p.add("Per");
p.add("Mohammed");

System.out.println(p.get(ran.next));
Kharbora
  • 85
  • 1
  • 2
  • 10
  • 1
    possible duplicate of [Pick a random element from a string array?](http://stackoverflow.com/questions/25150199/pick-a-random-element-from-a-string-array) – Thilo Oct 08 '14 at 08:54

1 Answers1

0

Try this..

Collection.shuffle(p);
System.out.println(p.get(0));
tarkikshah
  • 534
  • 6
  • 16
  • That will be unnecessarily inefficient. There is no need to shuffle entire collection when you just want to choose random element. – damgad Oct 08 '14 at 09:28