1. 程式人生 > >迭代器移除

迭代器移除

方法1:
//移除掉病理型別的
Iterator applicationFormListIte = applicationFormList.iterator();
while(applicationFormListIte.hasNext())
{
ApplicationForm applicationFormCurrent = applicationFormListIte.next();
String doctorAdviceType = applicationFormCurrent.getDoctorAdviceType();
if(doctorAdviceType != null && doctorAdviceType.equals(“病理類”))
{
applicationFormList.remove(applicationFormCurrent);
}
}
return applicationFormList;
方法二:
//移除掉病理型別的
Iterator applicationFormListIte = applicationFormList.iterator();
while(applicationFormListIte.hasNext())
{
ApplicationForm applicationFormCurrent = applicationFormListIte.next();
if(applicationFormCurrent != null)
{
String doctorAdviceType = applicationFormCurrent.getDoctorAdviceType();
if(doctorAdviceType != null && doctorAdviceType.equals(“病理類”))
{
applicationFormListIte.remove();
}
}
}
return applicationFormList;
方法1會報錯,二方法2可以正確的移除
具體分析見連結

https://www.cnblogs.com/dolphin0520/p/3933551.html