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.

3
  • is this possible in Android?? Just a quick search on google would have answered your question. Commented Mar 21, 2017 at 11:04
  • i know i have done but it always showing me error Commented Mar 21, 2017 at 11:04
  • 6
    Possible duplicate of How to generate random integers within a specific range in Java? Commented Mar 21, 2017 at 11:15

2 Answers 2

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()));
Sign up to request clarification or add additional context in comments.

Comments

0

Simple Kotlin Solution

myList.random()

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

Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.