1 回答

TA貢獻1788條經(jīng)驗 獲得超4個贊
在您的構(gòu)造函數(shù)中,您不需要,Random r因為您已經(jīng)有了private Random r;
其余的似乎正在工作。注意您的remove(int m)方法,以免用戶傳遞大于 ArrayList 大小的值,以避免出現(xiàn) IndexOutOfBoundsException。
import java.util.ArrayList;
import java.util.Random;
public class Cup {
ArrayList<Integer> c = new ArrayList<Integer>();
private Random r;
public Cup() {
c.add(1);
c.add(2);
c.add(3);
//here you should use your r attribute
r = new Random();
}
public int count() {
return c.size();
}
public int select() {
int index = r.nextInt(c.size());
return c.get(index);
}
public void remove(int m) {
c.remove(m);
}
}
添加回答
舉報