As I'm attempting to remove an item from a List by the items respective index value I'm realizing (in a "going nuts" sort of way), that again because of pass by referencing I'm getting unexpected results as a developer not used to that concept (learning to like it but not sure how to work around things like this).

Basically once I remove the first index value that then affects the list I'm removing items from so that the next index I want to remove is wrong. This continues perpetually causing the wrong items to be removed from my list.

List<Integer> null values = new List<>(0,2,4);
for(Integer i : nullValues){ 

What's the trick? How do I avoid this typically, at least in this small example here?

3 Answers 3


Try below,

List<Integer> nullvalues = new List<Integer>{0,2,4};
nullvalues.sort();//sorts in ascending order

//Iterate descending & remove one by one
for(Integer i=nullvalues.size()-1;i>=0;i--){     

Dont forget to accepts answer if its helpful. Tia


Try removing values from the end of the list first:

Integer sz = myList.size() - 1;
for (Integer i = sz; i >= 0; i--) {
  • This doesn't remove any specific index like I was attempting to do with my nullValues List, it simple removes every item except the last (or the first). I'll try the advice of going backwards though and see what I get Feb 5, 2015 at 0:07
  • In which case, create an ordered list of indexes you want to remove and process that list from the bottom up ...
    – user735
    Feb 5, 2015 at 0:27
  • So is there an easy way to reverse my nullValues list? Cause if I reverse it and then use it just as I am right now you're saying that would work? Feb 5, 2015 at 0:29
  • myList.sort() will sort the list ascending; you can then iterate through the list in reverse order (see above) to sort that list descending ...
    – user735
    Feb 5, 2015 at 0:38
  • I'm sorry to sound incompetent, but if I did fully understand I wouldn't be posting my question, perhaps you could modify your answer to reflect the adjustment I need to make? Cause when I iterate through my nullValue list backwards what am I to do with it? remove the item and then re-add it? I can't do that again because of the pass by referencing. Not trying to be a jerk but what sounds simple in theory is confusing with pass by referencing, hence my OP Feb 5, 2015 at 1:12

If I understand correctly you want to remove items from the List of myList that has the same index's as the null value list?

Is this a list of sObjects? If so use the List deepClone method to clone null values into the my list.

That way it will not be a shallow copy (by reference).


sObject[] myList = nullValues.deepClone(true);

for(integer x=0;x<nullvalues.size();x++){

Now the problem will be that once you remove the values from myList the index's will change so you will need to keep a count of that so you can reference it

So it becomes

Integer offset = 0;

sObject[] myList = nullValues.deepClone(true);

for(integer x=0;x<nullvalues.size();x++){

When it is over, nullValues will be unchanged....

This assumes I understood your question correctly. If I did not please clarify your question with additional code, what you expect to happen, etc...

  • I updated my code to show that nullValues is a list of Integers. Basically it is a collection of indexes from another collection that had null values in it, hoping to clean it up by looping through nullValues and removing that index Feb 5, 2015 at 5:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .