c******n 发帖数: 4965 | 1 【 以下文字转载自 SanFrancisco 讨论区 】
发信人: creation (努力自由泳50m/45sec !), 信区: SanFrancisco
标 题: a newbie java question
发信站: BBS 未名空间站 (Fri Jan 9 22:57:06 2009)
for a coding pattern like
for( Type element : myArrayList ) {
if ( checkSomeCondition(element) == true ) {
myArrayList.remove(element)
}
}
doesn't seem to work, because as the iterator goes on, the current link is
removed.
how do you normally do this simple coding pattern?
thanks | g*****g 发帖数: 34805 | 2 Instead of doing remove, do an add to a temporary collection,
then do a removeAll
【在 c******n 的大作中提到】 : 【 以下文字转载自 SanFrancisco 讨论区 】 : 发信人: creation (努力自由泳50m/45sec !), 信区: SanFrancisco : 标 题: a newbie java question : 发信站: BBS 未名空间站 (Fri Jan 9 22:57:06 2009) : for a coding pattern like : for( Type element : myArrayList ) { : if ( checkSomeCondition(element) == true ) { : myArrayList.remove(element) : } : }
| c******n 发帖数: 4965 | 3 got answer in Sanfrancisco board
the method to use is
to use an explicit iterator,
and do iterator.remove() instead of list.remove(value)
【在 g*****g 的大作中提到】 : Instead of doing remove, do an add to a temporary collection, : then do a removeAll
|
|