0

这是我使用 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
        }
    }
}

请帮忙。

4

3 回答 3

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
    
于 2012-08-02T11:36:26.890 回答
0

这是一个无限循环,外部循环取决于lastrecord程序中哪个没有设置为真

于 2012-08-02T11:29:42.183 回答
0

每当我写单词时,我都会抛出一个循环计数器while,如下所示:

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!"); }
于 2012-08-02T11:33:31.877 回答