-1

私は次のコードを使用しています:

boolean continueProcessing = true;  
boolean lastRecord = false;     
while (continueProcessing)  //can it be changed to while (continueProcessing == true), what's the advantage  

{  
if(refListItemItr.hasNext() || lastRecord)  
{  
continueProcessing = true;  
lastRecord = false;  
}  
else  
{  
continueProcessing = false;  
}  

if(continueProcessing)  
{  
lSynonymListType = objFactory.createSynonymListType();  
}  
}  

無限ループのシナリオがあるのではないかと思います。それが起こらないことをどのように確認すればよいですか。

4

1 に答える 1

2

これにより無限ループが発生する理由は、次の項目が存在するかどうかを確認し続けるためですが、実際にはそれを取得するために next() を呼び出すことはないため、内部ポインターは移動せず、最初の項目があるかどうかを確認するだけです。リスト内のアイテム。

とにかく、あなたはこれを過度に複雑にしています。あなたはただやっているはずです

while (refListItemItr.hasNext()){
  Object item = refListItemItr.next(); // change Object to the item's type
  // Presumably actually do something with the item here, which currently you're not...
}

または、さらに単純に

for (Object o : refListItemItr){
  // do stuff
}

そして、あなたの他の質問への答えとして、 と の間にはまったく違いはwhile(continueProcessing)ありませんwhile (continueProcessing == true)

于 2012-07-26T07:29:45.103 に答える