2012-07-26 46 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)