Quantcast
Channel: Ignite Realtime : Discussion List - All Communities
Viewing all articles
Browse latest Browse all 10742

Java for-each loop with inner-loop return not working as expected

$
0
0

Following Java code in package org.jivesoftware.smack.packet/Packet.java class is not working in Android as expected whether it is compiled with JDK 1.6 or 1.7. When the "if statement" is evaluated true, the statement "return ext;" is not being executed. Instead it executed the last statement "return null;".

public PacketExtension getExtension(String elementName, String namespace) {

    if (namespace == null) {

        return null;

    }

       

     for (PacketExtension ext : packetExtensions) {

        if ((elementName == null || elementName.equals(ext.getElementName())) && namespace.equals(ext.getNamespace())) {

                    return ext;

        }

    }

    return null;

}

 

Try to change the code to below; but neither of them is working. It seems that ext2 in last code example, although it is being assigned correctly when the if statement is evaluated true, it is destroyed prior to execute the last statement "return ext2;"

 

PacketExtension ext =null;
for(Iterator<PacketExtension> iterator = packetExtensions.iterator(); iterator.hasNext();){
    ext
= iterator.next();
   
if((elementName == null ||elementName.equals(ext.getElementName()))&& namespace.equals(ext.getNamespace()))
   
{
        
return ext;
   
}
}

returnnull;

 

-------------------------------------------------

and also:

 

acketExtension ext =null;
PacketExtension ext2 =null;

for(Iterator<PacketExtension> iterator = packetExtensions.iterator(); iterator.hasNext();){
    ext
= iterator.next();
   
if((elementName == null ||elementName.equals(ext.getElementName())&& namespace.equals(ext.getNamespace()))
   
{
     ext2
= ext;
         
break;
   
}
}
return ext2;


Viewing all articles
Browse latest Browse all 10742

Trending Articles