Solved

iterating over list and removing items

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 40
C# bracket error 3 30
HttpPostedFile 1 26
Problem to Alipay 10 22
Note: You must have administrative privileges in order to configure lead or case queues. Salesforce.com (http://www.Salesforce.com) is a cloud-based customer relationship management (CRM) system. It is widely used around the world by sales and ma…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

770 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