java.util.ConcurrentModificationException,如何避免

时间:2012-07-18 16:18:19

标签: java arraylist

以下是代码: - 请你告诉我它为什么会抛出异常以及如何避免这种情况,谢谢你的帮助......

public Collection getBeansFilteredByAuthorityAndApproach(ITaskContext context)throws CEAppException

{ Collection regApprAuthMapColl = this.getRegulatoryAuthorityApproachMap(context);//Collection of All approach combos

Collection finalAuthApprMapColl = new ArrayList();

ParameterSet pSets = context.getExecutionThread().getParameterSet();

//<RuntimeParameterSet> or list of <RuntimeParameters>

ArrayList apAR =pSets.getRuntimeParameterSet().getRuntimeParametersList();

int apSize = apAR.size() ;

for (int j = 0; j < apSize; j++)

{//1

//Individual <RuntimeParameterSet> or list of <RuntimeParameters>

RuntimeParameters rps = (RuntimeParameters) apAR.get(j);

if (Constants.CLONE_TYPE.equalsIgnoreCase(rps.getType())) //TODO :check if it required or not

{//2

//obtain a list of <RuntimeParameter> or indiviual <RuntimeParameters>

ArrayList rtParams = rps.getRuntimeParameterList();

int rtSize = rtParams.size();

for (int k = 0; k < rtSize; k++)

{//3 FOR EACH RUNTIME PARAMETER , i.e Approach & Authority

//Individual <RuntimeParameter>

Integer inputAuthority = null;

Integer inputApproach = null;

RuntimeParameter rtp = (RuntimeParameter) rtParams.get(k);

logger.debug("========== RuntimeParameter rtp="+rtp);

if (Constants.REGAUTHORITY.equals(rtp.getName()))

{

Integer regAuthority = ReferenceData.getRegAuthority(context,rtp.getValue());

inputAuthority = regAuthority;

} else if (Constants.REGAPPROACH.equals(rtp.getName()))

{

Integer regApproach = ReferenceData.getRegApproach(context,rtp.getValue()) ;

inputApproach = regApproach;

}

//FOR EACH RUNTIME PARAMETER FILTER THE COLLECTIONS

 

//FILTERING DATA

logger.debug("inputApproach :"+inputApproach);

logger.debug("inputAuthority :"+inputAuthority);

if(inputApproach == null)// if APPROACH IS NOT specified in the notification

{

if( inputAuthority == null)// if AUTHORITY IS NOT specified & APPROACH IS NOT specified

{ //Then run for all auhorities and approaches

finalAuthApprMapColl = regApprAuthMapColl;//returning all possible beans

}//end of if

else// if AUTHORITY IS specified & APPROACH IS NOT specified

{ //Then for the given authority, run for all approaches: B1,STD,IRB,AIRB

for (Iterator itr = regApprAuthMapColl.iterator(); itr.hasNext();)

{//looping through all possible values to select

RegulatoryAuthorityApproachMap raaMap = (RegulatoryAuthorityApproachMap)itr.next();

Integer fullAuthorityId = raaMap.getRegulatoryAuthorityId();

if(inputAuthority.intValue() == fullAuthorityId.intValue())// for a given authority

{

finalAuthApprMapColl.add(raaMap);//returning a list of all approaches for a given authority

}

}//end of for loop

}//end of else

}//end of if approach is null

else//if APPROACH IS specified in the notification

{

if(inputAuthority == null)// if AUTHORITY IS NOT specified && APPROACH IS specified

{//Then for the given approach, run for all authorities: FSA,EBK,BIS

for (Iterator itr = regApprAuthMapColl.iterator(); itr.hasNext();)

{//looping through all possible combos to select

RegulatoryAuthorityApproachMap raaMap = (RegulatoryAuthorityApproachMap)itr.next();

Integer fullApproachId = raaMap.getRegulatoryApproachId();

if(inputApproach.intValue() == fullApproachId.intValue())// for a given approach

{

finalAuthApprMapColl.add(raaMap);//returning a list of all authorities for a given approach

}

} //end of for loop

}//end of if

else// if AUTHORITY IS specified & APPROACH IS specified

{

for (Iterator itr = regApprAuthMapColl.iterator(); itr.hasNext();)

{//looping through all possible combos to select

RegulatoryAuthorityApproachMap raaMap = (RegulatoryAuthorityApproachMap)itr.next();

Integer fullApproachId = raaMap.getRegulatoryApproachId();

Integer fullAuthorityId = raaMap.getRegulatoryAuthorityId();

if((inputApproach.intValue() == fullApproachId.intValue()) && (inputAuthority.intValue() == fullAuthorityId.intValue()))// for a given approach

{

finalAuthApprMapColl.add(raaMap);//returning a list of all authorities for a given approach

}

} //end of for loop

}

}//end of else

}//3

}//2

}//1

if(finalAuthApprMapColl.size() == 0){throw new CEAppException("The Authority/Approach mapping is null"); }

else

return finalAuthApprMapColl;

}//end of method

1 个答案:

答案 0 :(得分:1)

您的代码非常不清楚,但从我可以解决的问题来看,您在给定集合上使用迭代器:

for(Iterator itr = regApprAuthMapColl.iterator(); itr.hasNext();)

在这种情况下,集合为regApprAuthMapColl。但是,您在迭代时也要修改集合,在for循环中,您有以下内容:

finalAuthApprMapColl.add(raaMap);

这是不允许的 - 你不能在迭代它的同时修改集合,并且可能是抛出异常的地方。