You need to sign in to do that
Don't have an account?
Alex.Acosta
IF Condition Bug?
I'm having some issue with some code and this seems like some sort of bug, can anyone explain to me why this is happening?
Map<String, List<String>> stringMap = new Map<String, List<String>>(); stringMap.put('key1', new List<String>{'value1', 'value2'}); for(String mapKey :stringMap.keySet()){ for(Integer i = 0; i <= stringMap.get(mapKey).size(); i++){ if(stringMap.get(mapKey).size() > i){ system.debug(stringMap.get(mapKey).get(i)); } if(stringMap.get(mapKey).size() > i++){ system.debug(stringMap.get(mapKey).get(i)); } /** * CODE BREAKS ON THIS IF CONDITION * WHICH IT SHOULD FAIL TO VALIDATE... * stringMap.get(mapKey).size() = 2 * i++ = 2
* This IF condition should not pass. **/ if(stringMap.get(mapKey).size() > i++){ system.debug(stringMap.get(mapKey).get(i)); } } }
++ after a variable is a "postfix" operator. In other words, it occurs after the rest of the statement is evaluated. This means your code looks like this:
Instead, use the less commonly used ++i notation:
All Answers
++ after a variable is a "postfix" operator. In other words, it occurs after the rest of the statement is evaluated. This means your code looks like this:
Instead, use the less commonly used ++i notation:
Thanks, I did not know that.