Solved

iterating over list and removing items

Posted on 2013-10-30
2
442 Views
Last Modified: 2016-02-17
I'm running into a logic problem and I'm hoping someone can help.  I using the Salesforce apex language, but it is similar to Java and C# so I'm posting in both the c# and java topic areas as the syntax is similar.

I'm using a simple for loop structure:

    		for(Integer i=0; i < contactLinesForPage.size(); i++) {
    			if(contactLinesForPage[i].selected == false) {
    				contactLinesForPage.remove(i);
    			}
    		}

Open in new window


I have a list of records and I'm checking to see which records have been selected.  If the record has not been selected, I'm removing it from the list.  So, I should end up with a list of only the records that have been selected.  

As the list size goes down and the iterator goes up, it does not end up removing all my items that were not selected from the list.

For example, let's say 6 records are returned and the user selects 1 record.  When I finish iterating over the list, I should just have 1 record, but I end up with 3 records (1 that was selected by the user and 2 records that were not selected).

I know my loop logic is flawed, but can someone help explain what I need to do to correctly iterate over my records and remove the items from the list that were not selected?

Any help is appreciated.
Thanks.
0
Comment
Question by:-Dman100-
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39612915
This is because the value for is changing when you remove items from the list so instead declare a List variable called selectedContactLinesForPage and add items from contactLinesForPage to it when you have detected they are selected.
for(Integer i=0; i < contactLinesForPage.size(); i++) {
    			if(contactLinesForPage[i].selected == true) {
    				SelectedContactLinesForPage.Add(contactLinesForPage[i]);
    			}
    		}

Open in new window

0
 
LVL 35

Expert Comment

by:mccarl
ID: 39613091
Either the above, or you can decrement the loop counter inside the if statement, such as ...
    		for(Integer i=0; i < contactLinesForPage.size(); i++) {
    			if(contactLinesForPage[i].selected == false) {
    				contactLinesForPage.remove(i);
    				i--;
    			}
    		}

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This video teaches viewers about errors in exception handling.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question