List_of_accounts[] = {acct0, acct1, acct2, acct3, acct4}; For(integer i=0; i< 5; i++){ If(I.isEvenNumber()){ List_of_accounts.remove(i); } } Using system debug new value of list is : acct1, acct2, acct4 and list out of bound error. The desired output after removing even numbers should be acct1, acct3. Can you explain the issue?

Let’s track it. Remove method accepts index to discard the value.

For i=0, it is even number value at index 0 removed from list  updated list values: acct1, acct2, acct3, acct4. Please remember after removal operation; indexes are recalculated based on the new order of list.

For i=1, it is not even number; Nothing happens.

For i=2, it is even number; value at index 2 will be removed from list  According to new order, acct3 is at index 2, so it will be removed. Updated list: acct1, acct2, acct4

For i=3, it is not even number; nothing happens

For i=4, it is even; but the new list contains only 3 elements, and a statement will try to remove 4th index, it will throw a list out of bound error.

About the author

By Rohit

Recent Posts

Recent Comments

Archives

>