2012-07-26 55 views
-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(); 
} 
} 

我懷疑有無限循環的情況。我應該如何確認它沒有發生。

+0

請格式化你的代碼之間沒有區別。 – 2012-07-26 06:46:51

+0

此代碼似乎並未實際執行任何操作......您想在此實現什麼功能?另外,refListItemItr的類型是什麼? – Braiba 2012-07-26 06:53:41

+0

我沒有寫完整的代碼。這很大。只是想知道這個情景能否完全消除? – dev 2012-07-26 07:15:52

回答

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)