Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

iterating over list and removing items

Posted on 2013-10-30
2
Medium Priority
?
465 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-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 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 36

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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

660 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