我试图循环一个arraylist并逐渐删除每3个索引元素.一旦它到达arraylist的末尾,我想将索引重置回到开头,然后再次循环遍历arraylist,再次每3个索引删除一个元素,直到arraylist中只剩下一个元素.
listOfWords是一个长度为3的数组,之前已填充.
int listIndex = 0;
do
{
// just to display contents of arraylist
System.out.println(listOfPlayers);
for(int wordIndex = 0; wordIndex < listOfWords.length; wordIndex++
{
System.out.print(“Player”);
System.out.print(listOfPlayers.get(wordIndex));
System.out.println(“”);
listIndex = wordIndex;
}
listOfPlayers.remove(listOfPlayers.get(listIndex));
}
while(listOfPlayers.size() > 1);
我试图实施几个小时但我仍然遇到麻烦.这是arraylist的元素发生了什么:
1, 2, 3, 4
1, 2, 4
1, 2
然后它在检查第三个元素(不再存在)时抛出’index out of bounds error’异常.一旦它到达最后一个元素,我希望它环绕到第一个元素并继续通过数组.我也希望它从它停止的地方开始,而不是从一开始就从arraylist中删除一个元素.