Java-使用2个迭代器解析ArrayList和可怕的ConcurrentModificationException
我正在搜索ArrayList,并与2个迭代器进行比较。我将值写入一个字符串缓冲区,该缓冲区最终将成为XML输出。当我解析这些值时,我正在检查匹配的itemIds。匹配通常是零件和图纸。一个零件可以有许多图纸。对于XML,我必须知道所有匹配项的类型和名称,并将值附加在一起 使用此ArrayList: itemId类型名称 1000件锤Java-使用2个迭代器解析ArrayList和可怕的ConcurrentModificationException,java,iterator,Java,Iterator,我正在搜索ArrayList,并与2个迭代器进行比较。我将值写入一个字符串缓冲区,该缓冲区最终将成为XML输出。当我解析这些值时,我正在检查匹配的itemIds。匹配通常是零件和图纸。一个零件可以有许多图纸。对于XML,我必须知道所有匹配项的类型和名称,并将值附加在一起 使用此ArrayList: itemId类型名称 1000件锤 1001件指甲 1000 dwg语义 1002零件尺 我的示例XML输出大致如下所示: <Master itemId=1000 type=part name=
1001件指甲
1000 dwg语义
1002零件尺 我的示例XML输出大致如下所示:
<Master itemId=1000 type=part name=hammer>
<Extra type=dwg name=semantic>
</Master>
<Master itemId=1001 type=part name=nail>
</Master>
<Master itemId=1002 type=part name=ruler>
</Master>
这是我的第一个循环
while (theBaseInterator.hasNext()){
ImportedTableObjects next = theBaseInterator.next();
currentEntry = next.identiferId;
currentType = next.typeId;
currentDatasetName = next.nameId;
compareInterator = tArray.listIterator(theBaseInterator.nextIndex());
compareEntriesofArray(currentEntry, currentType, currentDatasetName, compareInterator); <======= calling method for 2nd loop and compare check
}
while(baseInterator.hasNext()){
ImportedTableObjects next=baseInterator.next();
currentEntry=next.identiferId;
currentType=next.typeId;
currentDatasetName=next.nameId;
compareInterator=tArray.listIterator(baseInterator.nextIndex());
compareEntriesofArray(currentEntry、currentType、currentDatasetName、compareInterator);最简单的方法可能是创建另一个列表,在其中放置“匹配”的条目,然后只需对照该列表进行检查。将所有要删除的元素添加到新列表中
迭代后,调用:
coll1.removeAll (coll2);
不是使用迭代器和它们的hasNext/next,而是使用列表,您可以使用for循环从上到下进行迭代。删除元素(7)bevore访问元素(6)等等对我来说从来都不是问题,但我没有看到有人推荐它
这里有完整的代码
import java.util.*;
public class GuessGame
{
public static void main ( String [] args )
{
char [] ca = "This is a test!".toCharArray ();
List <Character> ls = new ArrayList <Character> ();
for (char c: ca)
ls.add (c);
show (ls);
// first method: remove from top/end and step backwise:
for (int i = ls.size () - 1; i >= 0; --i)
{
char c = ls.get (i);
if (c == 'i' || c == 'a' || c == 'e')
ls.remove (i);
}
show (ls);
// second approach: collect elements to remove ...
ls = new ArrayList <Character> ();
for (char c: ca)
ls.add (c);
show (ls);
// ... in a separate list and
List <Character> toRemove = new ArrayList <Character> ();
for (char c: ls)
{
if (c == 'i' || c == 'a' || c == 'e')
toRemove.add (c);
}
// ... remove them all in one go:
ls.removeAll (toRemove);
show (ls);
}
private static void show (List <Character> ls)
{
for (char c: ls)
System.out.print (c + " ");
System.out.println ();
}
}
您是否可以将已经处理过的项目存储在二级结构中,例如树形图
,然后在那里进行检查?您是否可以在此建议上进行更多扩展
import java.util.*;
public class GuessGame
{
public static void main ( String [] args )
{
char [] ca = "This is a test!".toCharArray ();
List <Character> ls = new ArrayList <Character> ();
for (char c: ca)
ls.add (c);
show (ls);
// first method: remove from top/end and step backwise:
for (int i = ls.size () - 1; i >= 0; --i)
{
char c = ls.get (i);
if (c == 'i' || c == 'a' || c == 'e')
ls.remove (i);
}
show (ls);
// second approach: collect elements to remove ...
ls = new ArrayList <Character> ();
for (char c: ca)
ls.add (c);
show (ls);
// ... in a separate list and
List <Character> toRemove = new ArrayList <Character> ();
for (char c: ls)
{
if (c == 'i' || c == 'a' || c == 'e')
toRemove.add (c);
}
// ... remove them all in one go:
ls.removeAll (toRemove);
show (ls);
}
private static void show (List <Character> ls)
{
for (char c: ls)
System.out.print (c + " ");
System.out.println ();
}
}
T h i s i s a t e s t !
T h s s t s t !
T h i s i s a t e s t !
T h s s t s t !