Hi all

When a programer needs to move several elements from a node depending on content the most intuitive way is:

for(int i; i < node.size(); i++)

{

     element = node.getElementAt(i);

     if(element.getAttribute().equals(“String”))

     {

          node.removeElement(element);

     }

}

This sulotion in most cases doesn’t work because when removing an element then the indexes of all next elements gets subtracted by one and then every removal of element causes the next loop iteration to skip over an element .

for example: when removing row with index 5 then the next element gets the index 5 when in the next loop iteration i = 6.

here or three good solutions:

1)

Works only with nodes with ‘Selection Cardinality’ set to 0..n OR 1..n

for(int i; i < node.size(); i++)

{

     element = node.getElementAt(i);

     if(element.getAttribute().equals(“String”))

     {

          node.setSelected(i, true);

     }

}

node.removeSelectedElements();

2)

In reverse looping the influenced elements are processed already

for(int i=node.size() – 1; i >= 0; i–)

{

     element = node.getElementAt(i);

     if(element.getAttribute.equals(“String”))

     {

          node.removeElement(element);

     }

}

3)

Not so recomended, in general any change of the loop index can cause problems and isn’t recomended.

for(int i; i < node.size(); i++)

{

     element = node.getElementAt(i);

     if(element.getAttribute().equals(“String”))

     {

          node.removeElement(element);

          i–;

     }

}

Good day

Shai

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply