IBM Content Manager, Version 8.5      Supports:  Oracle, DB2, C++, Java

Using the sequential iterator

You iterate over collection members using iterators.

The APIs have two types of iterators: dkIterator and DKSequentialIterator.

Java™

dkIterator, the base iterator, supports the next, more, and reset methods. The subclass DKSequentialIterator contains more methods. You create an iterator by calling the createIterator method on the collection. After creating the iterator, you can use its methods to traverse the collection. The following example shows how to use an iterator:

dkIterator iter = sq.createIterator(); //create an iterator for sq
Object member;
while(iter.more()) { //While there are more members
member = iter.next(); //move to the next member and get it
System.out.println(member);
.... 
}

C++

Iterators are provided to let you iterate over collection members. There are two types of iterators: the base iterator dkIterator, which supports the next, more, and reset functions; and its subclass DKSequentialIterator, which contains more functions. An iterator is created by calling the createIterator function on the collection. This function creates an iterator and returns it to you. Use the following code to iterate over a collection:

dkIterator* iter = sq.createIterator(); //create an iterator for sq
DKAny* member;
                                    // while there are more members
                                    // get the current member and
                                    //advance iter to the next member
while(iter->more()) {
    member = iter->next();

    cout << *member << endl;           //display it, if you want to
    ...                                //do other processing
    }
delete iter;                           //do not forget to delete iter

This code allows you to perform some operations on the current member before moving to the next member. Such an operation could be replacing a member with a new one, or removing it.

Example: Java

String st1 = "the new first member";
sq.replaceElementAt(st1, iter); //replace current member with a new one
....                              // or
sq.removeElementAt(iter);      // remove the current member
....

Example: C++

any = DKString("the new first member");

sq.replaceElementAt(any, *iter); //replace current member with a new one
...                              // or
sq.removeElementAt();            //remove the current member
...
Tip: When you remove the current member, the iterator is advanced to the next member.
When removing a member inside a loop, check it as in the following example. Create a new iterator when an item is deleted from the iterator.

Example: Java

   ....
   if (removeCondition == true)
     sq.removeElementAt(iter); //remove current member, do not advance the
                               //iterator since it is advanced to the next
                               //after the removal operation
   else
        iter.setToNext();   //if no removal, advance the iterator to the
   ....                    //next position

Example: C++

...
if (removeCondition == TRUE)
 sq.removeElementAt(*iter); //remove current member, do not advance iter
                            //since it is advanced to the next after
                             //the removal operation
else
    iter->setToNext();       //no removal, advance the iterator
...                            //to the next position


Feedback

Last updated: December 2013
dcmap081.htm

© Copyright IBM Corporation 2013.