2017-03-21 48 views
-2

隨機值我有一個String陣列:獲取從一個ArrayList

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

其中我轉換成一個數組列表:

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

我想從我的ArrayList中的隨機值。

+0

_is這可能在Android ?? _只需在Google上快速搜索就可以解答您的問題。 –

+0

我知道我已經做了,但它總是告訴我錯誤 – 007

+6

[如何在Java中的特定範圍內生成隨機整數?](http://stackoverflow.com/questions/363681/how-to-generate-random -integers中之特定範圍式-java的) –

回答

4

使用Random類來做到這一點,方法的參數nextInt是要返回的隨機數的界限。它必須是正面的:

Random random = new Random(); 
yourList.get(random.nextInt(yourList.size()));