Solved

iterating over list and removing items

Posted on 2013-10-30
2
435 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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

911 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now