0

I have a String array:

String [] array={"1","2","3","4","5","6","7","8","9"};

Which I convert into an array list:

 ArrayList<String> al = new ArrayList<String>(Arrays.asList(array));

I want to get a random value from my ArrayList.

Ivan Semkin
  • 162
  • 1
  • 13
007
  • 486
  • 5
  • 15
  • _is this possible in Android??_ Just a quick search on google would have answered your question. – B001ᛦ Mar 21 '17 at 11:04
  • i know i have done but it always showing me error – 007 Mar 21 '17 at 11:04
  • 6
    Possible duplicate of [How to generate random integers within a specific range in Java?](http://stackoverflow.com/questions/363681/how-to-generate-random-integers-within-a-specific-range-in-java) – Manoj Perumarath Mar 21 '17 at 11:15

2 Answers2

5

Use Random class to do that, argument of method nextInt is the bound on the random number to be returned. It must be positive:

Random random = new Random();
yourList.get(random.nextInt(yourList.size()));
miljon
  • 2,021
  • 1
  • 12
  • 18
0

Simple Kotlin Solution

myList.random()

random() is a default extension function included in base Kotlin on the Collection object.

Documentation

Gibolt
  • 24,018
  • 9
  • 129
  • 89