0

I have a for loop condition in my code as below:

for(int i=0; contactInfoRecordArray[i].get_170_Cntct_Sqn() != 0 && 
             i<contactInfoRecordArray.length ; i++){
    insuranceObjectKey = new Integer(contactInfoRecordArray[i].get_170_Cntct_Insobj_Sqn());
        if(insuranceObjectMap.containsKey(insuranceObjectKey) && insuranceObjectMap.get(insuranceObjectKey) != null) {
            serviceUseMap = (HashMap)(insuranceObjectMap.get(insuranceObjectKey));
        } else {
            serviceUseMap = new HashMap();
            insuranceObjectMap.put(insuranceObjectKey, serviceUseMap);
        }
        //second level (Service eg. Wildfire)
        serviceUseKey = new Integer(contactInfoRecordArray[i].get_170_Cntct_Srvcuse_Sqn());
        if(serviceUseMap.containsKey(serviceUseKey) && serviceUseMap.get(serviceUseKey) != null) {
            contactMap = (HashMap)serviceUseMap.get(serviceUseKey);
        } else {
            contactMap = new HashMap();
            serviceUseMap.put(serviceUseKey, contactMap);
        }

        //third level (ContactAssignmentType)
        contactKey = new Integer(contactInfoRecordArray[i].get_170_Cntct_Sqn());
        if(contactMap.containsKey(contactKey) && contactMap.get(contactKey) != null) {
            contactAssignment = (ContactAssignmentType)contactMap.get(contactKey);
        } else {
            contactAssignment = ContactAssignmentType.Factory.newInstance();
            contactAssignment.setPriority(contactInfoRecordArray[i].get_170_Cntct_Prty_Seq_N());

            contactAssignment.setRole(ServiceEligibilityReferenceDataUtility.getInstance().getContactAssignmentType_Role(contactInfoRecordArray[i].get_170_Cntct_Role_C()));
            ContactType contact = contactAssignment.addNewContact();
            contact.setName(contactInfoRecordArray[i].get_170_Cntct_Na());
            contactMap.put(contactKey, contactAssignment);
        }

        //fourth level (Communication Info)
        if(contactAssignment.getContact().getCommunicationAggregate() != null) {
            communicationAggr = contactAssignment.getContact().getCommunicationAggregate();
        } else {
            communicationAggr = contactAssignment.getContact().addNewCommunicationAggregate();
        }

        CommunicationType communication = communicationAggr.addNewCommunication();
        communication.setPriority(contactInfoRecordArray[i].get_170_Commu_Prty_Seq_N());
        communication.setValue(contactInfoRecordArray[i].get_170_Commu_Dvc_V());
        communication.setUsage(ServiceEligibilityReferenceDataUtility.getInstance().getCommunicationType_Usage(
                contactInfoRecordArray[i].get_170_Commu_Dvc_Loc_Na().trim()));

        communication.setType(
                ServiceEligibilityReferenceDataUtility.getInstance().getCommunicationType_Type(
                        contactInfoRecordArray[i].get_170_Commu_Dvc_Na(), contactInfoRecordArray[i].get_170_Commu_Dvc_Mode_Na()));

} The contactInfoRecordArray.length = 100. Now when value of i reaches 100, I get an Array Out Of bound Exception.

If I reverse the condition in &&, it just works fine - please see code below:

for(int i=0; i<contactInfoRecordArray.length && 
             contactInfoRecordArray[i].get_170_Cntct_Sqn() != 0 ; i++)

I'm not sure why it works. Any insight would be greatly appreciated.

Priyanka
  • 51
  • 1
  • 1
  • 9
  • Your code samples seem to be truncated. Can you format the code correctly so that we can see it? – ManoDestra Mar 22 '16 at 20:31
  • If with first snippet , cntct_sqn is 0 - will value of i be still incremented ? – Priyanka Mar 22 '16 at 20:41
  • I'm actually rather dubious about the value of i. I think it may be being incremented elsewhere inside the for loop. Or perhaps in that additional function call inside the definition of the for loop itself. The value of i should only increment if BOTH conditions are true. Could you perhaps show us all of the code for the for loop and possibly the code for the get_170_Cntct_Sqn() method? – ManoDestra Mar 22 '16 at 20:45
  • ya, so how does the order change in && condition work . Not sure about it – Priyanka Mar 22 '16 at 20:48
  • Because if the first part fails in an AND operation, then it will not move on to the second part. That's why we really need to see the rest of the for loop and the contents of the method that you're calling inside the for signature. – ManoDestra Mar 22 '16 at 20:49
  • More specifically, [this answer](http://stackoverflow.com/a/8759917/256196) answers your question. – Bohemian Mar 22 '16 at 21:41
  • That doesn't actually apply here though. It's an && operation. So, it works regardless of which way round his operators are. The value of i is only incremented if the value of the && operation is true. And BOTH sides of the && need to be evaluated for it to be true. If the first part were false, then the for loop would stop and he would not receive the out of index error. Something else is causing his issue here. I initially thought this as well, but it's clearly not the problem. – ManoDestra Mar 22 '16 at 21:44
  • If array[i].getFoo() != 0 returns true , it will move to length part which does not satisfy the criteria and it should exit the loop but it gives array out of bound. – Priyanka Mar 23 '16 at 14:39
  • @ ManoDestra - any clue from the code? – Priyanka Mar 25 '16 at 19:20

0 Answers0