Skip to main content
Modified info
Source Link

Answer from @Adrian is good, but on a different note the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size();-1; i <i>=0 cnt;; i++i--) {
    Account a = accts[i];
}

Answer from @Adrian is good, but on a different note the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size(); i < cnt; i++) {
    Account a = accts[i];
}

Answer from @Adrian is good, but on a different note the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = accts.size()-1;  i>=0 ; i--) {
    Account a = accts[i];
}
added 1 character in body
Source Link
Jayant Das
  • 30.7k
  • 3
  • 42
  • 68

Answer from @Adrian is good, but on a different notnote the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size(); i < cnt; i++) {
    Account a = accts[i];
}

Answer from @Adrian is good, but on a different not the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size(); i < cnt; i++) {
    Account a = accts[i];
}

Answer from @Adrian is good, but on a different note the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size(); i < cnt; i++) {
    Account a = accts[i];
}
Source Link

Answer from @Adrian is good, but on a different not the reason why you're getting this error was because your code attempts to modify a collection while it is being iterated in the for each loop, which is not allowed. Read about Read-only Collections

If you need to modify the List or Set while iterating over it, use a simple for loop with a counter instead of the Set or List iteration.

for (Integer i = 0, cnt = accts.size(); i < cnt; i++) {
    Account a = accts[i];
}