1 回答

TA贡献1788条经验 获得超4个赞
在您的构造函数中,您不需要,Random r因为您已经有了private Random r;
其余的似乎正在工作。注意您的remove(int m)方法,以免用户传递大于 ArrayList 大小的值,以避免出现 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);
}
}
添加回答
举报