2012-08-02 191 views
0

这是一种使用while循环的方法。我强烈怀疑无限循环是可能的。如何检查并消除?我们如何确保while循环不会是无限循环?

我在这里使用了两个while循环。我可以完全拆除while循环吗?

public class ReferenceListSaveHandler 
{ 
public PublishReferenceListUpdates populateResponse(SearchQueryResponse pSearchQueryResponse,SearchQueryResponse pListResponse, ObjectFactory objFactory)   throws IOException, ParserConfigurationException, SAXException,SipException, Exception 
    { 
     ReferenceDataProcessor lRefPro = new ReferenceDataProcessor(); 
     PublishReferenceListUpdates lResponse = null; 
     Record listRecord = null; 
     ReferenceDataListItemListType lReferenceDataListItemListType = objFactory 
      .createReferenceDataListItemListType(); 
     ReferenceDataListType lReferenceDataListType = null; 
     ReferenceDataListItemType lReferenceDataListItemType = null; 
     boolean ifSynonym = false; 
     String lRowIdObject = null; 
     final int lRowIdLength = 14; 

     if (refListItemItr.hasNext()) 
     { 
      Record record = (Record)refListItemItr.next(); 
      boolean continueProcessing = true; 
      boolean lastRecord = false; 
      while (continueProcessing) 
      { // first use of while loop 
       if (refListItemItr.hasNext() || lastRecord) 
       { 
        continueProcessing = true; 
        lastRecord = false; 
       } 
       else 
       { 
        continueProcessing = false; 
       } 

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

        Field itemLSIDField = record 
         .getField(ReferenceDataConstants.FIELD_COMPOUND_ASSET_ID); 

        if (itemLSIDField == null) 
        { 
         continue; 
        } 
        else 
        { 
         currentItemLSID = itemLSIDField 
          .getStringValue().trim(); 
        } 
        lReferenceDataListItemType = objFactory 
         .createReferenceDataListItemType(); 
        lReferenceDataListItemType = setListDetails(record, 
         lReferenceDataListItemType, 
         lId, lName, objFactory); 


        while (refListItemItr.hasNext() 
          && continueProcessing) 
        { // second use of while loop 
         SynonymType lSynonymType = null; 
         if (continueProcessing) 
         { 
          if (lSynonymType != null) 
           lSynonymListType.getSynonym().add(lSynonymType); 
         } 
        } 
        continueProcessing = true; 
       } 
      }//while loop 
     } 
    } 
} 

请帮忙。

回答

1

问题一:refListItemItr,不管它是什么(未显示其定义),有一个.next()方法,但它从来没有所谓的循环中。

问题二:如果你进入它的第二个while环始终是无限的:

while (refListItemItr.hasNext() 
     && continueProcessing) 
{ 
    SynonymType lSynonymType = null; 
    if (continueProcessing) 
    { 
     if (lSynonymType != null) 
      lSynonymListType.getSynonym().add(lSynonymType); 
    } 
} 

无论refListItemItr.hasNext(),也可以continueProcessing这个循环中改变。这就是为什么它是一个无限循环。

编辑: 我不能告诉你如何改变代码,因为有很多可能的方式来改变它,这取决于应该发生什么。你可以尝试这个,但它可能会或可能不会工作:

  1. 完全删除第二个循环(它现在什么都没做)。
  2. 只是第一圈结束前加入这一行:

    record = (Record)refListItemItr.next(); // ADD THIS LINE 
    } //while loop BEFORE THIS LINE 
    
+0

你好,我只是在开玩笑:很好的分析 – Alex 2012-08-02 11:37:38

+0

实际上它被调用了Record record =(Record)refListItemItr.next (); 还有一件事,为什么既不能refListItemItr.hasNext(),也不continueProcessing在循环内部进行更改?而且,我该如何纠正它? – dev 2012-08-02 12:48:15

+0

它被称为外**循环。 **在** while(continueProcessing)'行之前。 'hasNext'和'continueProcessing'不能在第二个循环内部改变,因为你没有改变它们(你不是程序员,是吗?)。 – Alex 2012-08-03 08:09:47

0

这是一个无穷远循环,外循环依赖于未beeing设置为true,在程序

+0

这是无限的,但不是因为'lastRecord'变量(它总是'FALSE'因此'如果(refListItemItr.hasNext ()|| lastRecord)'相当于'if(refListItemItr.hasNext())' – Alex 2012-08-02 11:31:49

0

我扔在一个循环计数器,每当我写的字while,这样在任何地方lastrecord

int loopCounter = 0, maxLoops = 1000; //just an example 
while((loopCounter++) < maxLoops && (myConditions)) 
{ 
    // loop away, this can't ever be infinite 
} 

或本

int loopCounter = 0, maxLoops = 1000; //just an example 
do  
{ 
    // loop away, this can't ever be infinite 
} 
while((loopCounter++) < maxLoops && (myConditions)) 

当然,maxLoops总是尺寸为幅度较大的日订单迭代的任何合理数量的......然后,我添加一个检查:

if (loopCounter == maxLoops) { throw new InvalidOperationException("Infinite loop detected!"); }