Solved

iterating over list and removing items

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.NET Error 7 42
XML to SQL Table using c# 5 46
Code works but how can I download the file? 20 46
Chat Room 1 26
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
I showed you how to use console view (HERE (http://www.experts-exchange.com/articles/18379/Getting-Started-and-Using-the-Salesforce-com-Console.html)) -– but how do you set it up on the admin side of Salesforce? Note that you have to have Admin leve…
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…
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…

705 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

19 Experts available now in Live!

Get 1:1 Help Now