在Netbeans上执行Java项目时出现一个奇怪的错误。我有以下在main中执行的功能:
public ArrayList<String> getPlayersName(){
ArrayList<String> list = new ArrayList<>();
int players;
System.out.println("Number of players:");
players = in.nextInt();
for(int i=0; i<players; i++){
System.out.println("Player name:");
String s = in.next();
list.add(s);
}
return list;
}
执行项目时,出现以下错误:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
我提供的输入非常简单:2,a,b。奇怪的是,有时使用相同的参数,程序可以正常执行。
主程序:
public static void main(String args[]) {
QytetetTextView ui = new QytetetTextView();
controller.setPlayersName(ui.getPlayersName());
int chosenOp, casillaElegida = 0;
boolean needChooseBox;
do {
chosenOp = ui.elegirOperacion();
System.out.println(controller.getEstado());
needChooseBox = controller.needChoseBox(chosenOp);
if (needChooseBox)
casillaElegida = ui.chooseBox(chosenOp);
if (!needChooseBox || chosenBox >= 0)
System.out.println(controlador.runOp(chosenOp, chosenBox));
} while (1 == 1);
}