2013-03-03 85 views
0
for(PatientProcedures s: PatientProceduresList) 
     { 

      if(Num == s.getAccountNumber()) 
      { 
       PatientProceduresList.remove(s); 
       break; 
       //without break it stops cause of loop 


      } 
     } 

反正有這個嗎?它適用於中斷,但我需要繼續並繼續對陣列列表的其他部分做同樣的事情。ArrayList爲循環刪除多於1個?

+0

如果刪除'break;',它將刪除任何與Num匹配的s值。 – christopher 2013-03-03 19:54:54

+2

如果他刪除了中斷,那麼它將拋出一個'ConcurrentModificationException'。 – pickypg 2013-03-03 19:55:16

+0

除非使用迭代器,否則在遍歷數組時不能從數組列表中刪除。查看javadoc以獲取更多信息。 – 2013-03-03 19:55:16

回答

4

爲了做到這一點,您必須使用Iterator

Iterator<PatientProcedures> iterator = list.iterator(); 

while (iterator.hasNext()) 
{ 
    PatientProcedures s = iterator.next(); 

    if (wantToRemove) 
    { 
     iterator.remove(); 
    } 
} 

這將避免ConcurrentModificationException當你做一個for每個循環存在。

+0

工作就像一個魅力!謝謝! – 2013-03-03 20:07:21