我厌倦了制作 super 基本的宾果游戏(不需要过于复杂的东西)
package bingo;
import java.util.*;
public class Bingo {
public static void main(String[] args) {
Random rn = new Random();
ArrayList bingo = new ArrayList();
final int MAX = 50;
int no = rn.nextInt(49);
boolean finished = false;
for(int i = 0; i < MAX; i++){
bingo.add(i);
}
while(!finished){
// Keep the number it can generate the same size as the array-list
no = rn.nextInt(bingo.size());
这是哪里出了问题吗?它不是删除数字(例如“10”),而是删除数组中位置 10 的元素吗?
if(bingo.contains(no)){
System.out.println(no);
bingo.remove(no);
}
if(bingo.isEmpty()){
finished = true;
}
}
}
}
最佳答案
bingo.remove(no)
调用 remove(int index)
,它会删除索引为 no
的元素,而不是删除索引为 no
的元素值为否
。如果要删除值为 no 的元素,则需要使用 remove(Object o)
,它需要引用类型。例如,bingo.remove(Integer.valueOf(no));
。
关于java - 从数组列表中删除元素不起作用,发生重复?(基本宾果游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28395223/