-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

The reason this will cause an infinite loop is that you keep checking if the next item exists, but you never actually call next() to retrieve it, so the internal pointer doesn't move along and you're just checking if there's a first item in the list.

Regardless, you're overcomplicating this. You should just be doing

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...
}

Or, even more simply

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

And in answer to your other question, there is absolutely no difference between while(continueProcessing) and while (continueProcessing == true)

于 2012-07-26T07:29:45.103 回答