I have a an array of objects and would like to be able to ran开发者_Python百科domly choose one from the list when a button is pressed. How would you do that in Android?
Do something like this inside your onClickListener
Random rand = new Random();
int selector = rand.nextInt(yourList.length);
yourList.get(selector);
Something like that.
EDIT: Actually if it is an ArrayList then it will be more like this
Random rand = new Random();
int selector = rand.nextInt(yourList.size());
yourList.get(selector);
I use java.util.random.
It's basically plain java at this point. You can use java.util.nextInteger().
Is this something you are looking for?
Random r = new Random();
E element;
int rand = r.nextInt(array.length);
element = array[rand];
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论