In apex, how can I remove an element from a list being looped?
List<String> companyinfos=new List<String>();
companyinfos.add('a1');
companyinfos.add('a2');
companyinfos.add('a3');
companyinfos.add('b1');
companyinfos.add('b2');
integer index=0;
For(String s: companyinfos)
{
if(s.contains('a')){
companyinfos.remove(index);
index--;
}
index++;
}
System.debug(companyinfos);
ERROR: Cannot modify a collection while it is being iterated.