[英]ConcurrentModificationException when attempting to get an element from a List
So I am trying to create a simple Card game and to do this I have created a Player class that holds a name and the Player's hand. 因此,我尝试创建一个简单的纸牌游戏,并为此创建了一个Player类,其中包含一个名称和Player的手。
public class Player {
private List<Card> hand;
private String name;
public Player(String name){
this.name = name;
}
public void drawHand(List<Card> hand){
System.out.println(this.name + " draws a hand.");
this.hand = hand;
}
public String throwingError(){
Card c = this.hand.get(0);
return c.getRank();
}
}
And here is the Engine running the game 这是运行游戏的引擎
public static void main(String[] args) {
int numberOfPlayers = Integer.parseInt(args[0]);
players = new Player[numberOfPlayers];
deck = createDeck();
for(int i = 0; i < numberOfPlayers; i++){
players[i] = new Player("Player" + (i+1));
}
for(Player player : players){
List<Card> hand = deck.dealHand();
player.drawHand(hand);
}
String cardRank = players[turn - 1].throwingError();
}
The code is throwing a ConcurrentModificationException when it gets to the last line of the main function, which baffles me since no elements are being iterated or deleted from the hand field in the Player class. 当代码到达主函数的最后一行时,该代码将引发ConcurrentModificationException,这使我感到困惑,因为没有从Player类的hand字段中迭代或删除任何元素。
The getRank() method is part of a different class called Card. getRank()方法是另一个称为Card的类的一部分。 It's a simple getter
这是一个简单的方法
public String getRank() {
return rank;
}
And here is the stack trace: 这是堆栈跟踪:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(ArrayList.java:1231)
at java.util.ArrayList$SubList.listIterator(ArrayList.java:1091)
at java.util.AbstractList.listIterator(AbstractList.java:299)
at java.util.ArrayList$SubList.iterator(ArrayList.java:1087)
at java.util.AbstractCollection.toString(AbstractCollection.java:454)
at java.lang.String.valueOf(String.java:2981)
at java.io.PrintStream.println(PrintStream.java:821)
at Player.getRank(Player.java:22)
at GameEngine.nextTurn(GameEngine.java:32)
at GameEngine.main(GameEngine.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
In this code: 在此代码中:
for(Player player : players){
List<Card> hand = deck.dealHand();
player.drawHand(hand);
}
your dealHand()
method is returning a sublist in each iteration, while at the same time adding to the backing list. 您的
dealHand()
方法在每次迭代中都返回一个子列表,同时将其添加到后备列表中。 This is a structural modification of the backing list and, as the Javadoc for sublist()
says, the behaviour of methods of the sublist is undefined after a structural modification of the backing list. 这是对后备列表的结构性修改,并且正如
sublist()
的Javadoc所说,在对后备列表进行结构性修改后,未定义子列表的方法的行为。 Thus, the get() operation on the sublist throws. 因此,对子列表的get()操作将引发。
Either create the whole list in one go and then divide it into sublists, or return copies of each sublist. 一次性创建整个列表,然后将其划分为子列表,或者返回每个子列表的副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.