2013-03-04 72 views
0

您好我所有的代碼Java的ArrayList的乘法

solution first = mylist.remove((int)(Math.random() * mylist)); 

以下線,給我一個錯誤,說明

The operator * is undefined for the argument type(s) double, ArrayList<solution> 

我想從我的ArrayList

在我的ArrayList中刪除一個隨機數

任何幫助將appriciated。

+0

請包括'mylist'的聲明 – amphibient 2013-03-04 18:15:27

+0

@foampile從錯誤消息中,'myList'被聲明爲'ArrayList ' – 2013-03-04 18:16:48

回答

4

它看起來像你試圖從列表中刪除一個隨機元素。要使用隨機索引覆蓋所有元素,您需要列表大小。

將數字乘以ArrayList沒有任何意義。您無法通過直接在代碼中指定您的列表來獲取列表的大小。在列表中調用size()方法。這返回可以相乘的int

+0

是的謝謝!我錯過了那個操作員!感謝您的親切幫助! – user1816464 2013-03-04 18:21:04

0

你需要找到你的列表大小的範圍內的隨機數

final Random random = new Random(); 

mylist.remove(random.nextInt(myList.size())); 

確保您創建一個Random並將其存儲爲否則會重複創建相同數量的(這是唯一的僞隨機) 。

另外,nextInt方法不包括上限,因此mylist.size()不會返回無效索引。