문맥없는 문법으로 읽고 모든 비 터미널의 FirstSets를 반환하는 Java 프로그램을 작성하는 과제가 있습니다. 내 FirstSet로 재귀 적 접근 방식을 촬영했습니다() 메소드 : 그러나Java - 재귀 메서드 및 중첩 반복기를 사용하는 ConcurrentModificationException
public static ArrayList<String> firstSet(String nonTerminal){
ArrayList<String> first = new ArrayList<String>();
//Find initial elements of FirstSet
for(String[] current : prodRules) {
if (current[0].equals(nonTerminal)){ //if the production rule is for that non-terminal
if(current.length > 2) {
first.add(current[2]); //first element after "-->" is added to FirstSet
} else
first.add("eps");
}
}
//Add the FirstSet of each element in the initial FirstSet
ArrayList<String> copy = first; //to avoid ConcurrentModificationException
Iterator<String> it1 = copy.iterator();
while(it1.hasNext()) { //recursively find each FirstSet of the initial elements
String s = it1.next();
System.out.println("FIRST("+s+")={");
ArrayList<String> nestedFS = firstSet(s);
Iterator<String> it2 = nestedFS.iterator();
while(it2.hasNext()) {
String f = it2.next();
if(!first.contains(f))
first.add(f);
System.out.print(" "+f+" ");
}
System.out.print("}");
}
return first;
}
, 나는 라인의 해, ConcurrentModificationException 오류가 계속 :
String s = it1.next();
ArrayList<String> nestedFS = firstSet(s);
지금까지 내가, 난 말할 수
현재 반복하고있는 목록을 수정하지 않습니다. 오히려 나는 사본을 통해 반복하고있다. 중복 메모리 사용이나 속도에 신경 쓰지 않아도 작동하려면이 기능이 필요합니다.
내가 여기서 잘못하고있는 것에 대한 단서가 있습니까? 어떤 도움이라도 대단히 감사합니다.
이것은 해결책이었습니다! 고맙습니다. – NeroTheHero