2013-09-10 91 views
0

我正在写一个解决Mastermind游戏的程序。该计划的要点是要列出所有可能的解决方案,并在每次猜测不正确之后,从列表中删除任何至少不会提供该解决方案的内容。这个方法用来比较两个字符串(guess和strFromArray),看它们是否得到相同的值。但是,我收到一个错误,我无法弄清楚为什么。任何帮助,将不胜感激。我不明白为什么我有这个错误:ConcurrentModificationException

Exception in thread "main" java.util.ConcurrentModificationException 
at java.util.ArrayList$Itr.checkForComodification(Unknown Source) 
at java.util.ArrayList$Itr.next(Unknown Source) 
at Game.shortenTheList(Game.java:88) 
at Game.guess(Game.java:76) 
at Game.play(Game.java:40) 
at Game.main(Game.java:23) 


/* 
* Compares the two strings. If they would get the same output, return false. If they would get a different output, return true. 
*/ 
public boolean compare(String guess, String strFromArray, int black, int white) 
{ 
    int white2 = 0; 
    int black2 = 0; 

    char[] arr1 = guess.toCharArray(); 
    char[] arr2 = strFromArray.toCharArray(); 

    for(int i=0; i<guess.length(); i++) 
    { 
     if(arr1[i] == arr2[i]) 
     { 
      black2 ++; 
      arr1[i] = '$'; 
      arr2[i] = '%'; 
     } 
    } 

    for(int i=0; i<guess.length(); i++) 
    { 
     for(int j=0; j<strFromArray.length(); j++) 
     { 
      if(arr1[i] == arr2[j]) 
      { 
       white2++; 
       arr1[i] = '!'; 
       arr2[j] = '@'; 
      } 
     } 
    } 

    if(black == black2 && white == white2) 
     return false; 

    else 
     return true; 
} 

/* 
* Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output. 
*/ 
public void shortenTheList(String guess, int black1, int white1) 
{ 
    for (String str : possibleSolutions) 
    { 
     if(compare(guess, str, black1, white1)) 
     { 
      possibleSolutions.remove(str); 
     } 
    } 
} 
+1

你能证明它来自这个代码片断? –

+2

Finny,没有使用迭代器。你是通过多线程访问它吗?你能至少发布stacktrace的前4行吗? – hexafraction

+0

在询问异常情况时,请务必发布堆栈跟踪。 – chrylis

回答

8

一旦你打开一个Iterator(使用for(String str: possibleSolutions),底层集合(possibleSolutions)任何修改时除外通过调用Iteratorremove你做隐含会造成ConcurrentModificationException,这是非常清楚地记录在采集班

如果您需要从集合中删除的项目,使用一个明确的Iterator

Iterator<String> it = possibleSolutions.iterator(); 
while(it.hasNext()) { 
    if(compare(guess, it.next(), black1, white1)) 
     it.remove(); 
} 

正如@allprog所指出的,当你有这样一个明确的“过滤”问题时,功能性方法会更好。在选择Java 8之前,使用Guava的Iterables#filterIterables#removeIf可能是一个不错的选择;你只需将你的compare方法包装起来并将其传入。

+1

如果remove()没有被调用,它会更好。当列表被过滤而不是被修改时使用功能方法通常是更安全的解决方案。谷歌Guava的“Iterables”课程是这方面最佳实践的“金矿”。 – allprog

2
/* 
* Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output. 
*/ 
public void shortenTheList(String guess, int black1, int white1) 
{ 
    for (String str : possibleSolutions) 
    { 
     if(compare(guess, str, black1, white1)) 
     { 
      possibleSolutions.remove(str); 
     } 
    } 
} 

这是你的问题。

相反,使用:

/* 
* Shortens the list of possible solutions by eliminating everything that wouldn't get at least the given output. 
*/ 
public void shortenTheList(String guess, int black1, int white1) 
{ 
    Iterator<String> it = possibleSolutions.iterator(); 
    while(it.hasNext()) 
    { 
     String str = it.next(); 
     if(compare(guess, str, black1, white1)) 
     { 
      it.remove(); 
     } 
    } 
} 

这是唯一干净的方式从你目前遍历集合中删除的对象。另一种较不优雅的方式是创建一个单独的String列表来删除和迭代该列表。

1

当您使用foreach循环遍历它时,您无法修改列表possibleSolutions

更改您的代码是这样的:

public void shortenTheList(String guess, int black1, int white1) 
{  
    for(Iterator<String> it = possibleSolutions.iterator(); it.hasNext()){ 
     String str = it.next(); 
     if(compare(guess, str, black1, white1)){ 
      it.remove(); 
     } 
    } 
} 
0

像chrylis说,你不能没有使用迭代器中删除从集合的元素。

例如:

public void shortenTheList(String guess, int black1, int white1) 
{ 
for (String str : possibleSolutions) 
{ 
if(compare(guess, str, black1, white1)) 
{ 
possibleSolutions.remove(str); 
} 
} 
} 

应该是:

public void shortenTheList(String guess, int black1, int white1) 
{ 
Iterator i = possibleSolutions.Iterator(); 
while(i.hasNext()) 
{ 
String str = (String) i.next(); 
if(compare(guess, str, black1, white1)) 
{ 
i.remove(); 
} 
} 
} 
相关问题