members are unique. The values of the members serve as the key according to
which the data is automatically ordered. Thus, it differs from a List collection
class where the members are placed into a specific position, and not ordered
automatically by their value.
Set members may be of any X++ type. All members in the set must have the
same type.
When a value that is already stored in the set is added again, it is ignored and
does not increase the number of members in the set.
The following methods are commonly used on Set objects:
• add(anytype) - inserts a value into the set.
• remove(anytype) - removes a value from the set.
• in(anytype) - determines whether a particular value is a member of
the set.
• elements() - returns the number of members contained in the set.
• getEnumerator() - returns a SetEnumerator object for this Set
object.
The SetEnumerator class allows you to traverse through the members within a
set. The following methods are commonly used on SetEnumerator objects:
• current() - retrieves the value currently pointed to in the set.
• moveNext() - moves the enumerator to the next value in the set. Set
enumerators start before the first value in the set, so moveNext()
must be called to make it point to the first value in the set.
• reset() - moves the enumerator to the start of the set.
The following example demonstrates how to create two sets, and then compares
each set to detect any shared members and remove them.
1. Create two new Set objects, specifying the data type they will
contain.
2. Add members to the Set objects, using the Set.insert() method.
3. Set a SetEnumerator object for one of the Sets, using theSet.getEnumerator() method.
4. Go to the start of the set, using SetEnumerator.reset().
5. Move through the members in the set, usingSetEnumerator.moveNext().
6. Pull the value of the current member in the set, usingSetEnumerator.current().
7. Search for the value in the second set, using Set.in(). If found,
remove it using Set.remove().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | Set setOne; Set setTwo; SetEnumerator enumerator; Int value; setOne = new Set(types::Integer); setOne.add(1); setOne.add(2); setOne.add(3); setTwo = new Set(Types::Integer); setTwo.add(3); setTwo.add(4); setTwo.add(5); enumerator = setOne.getEnumerator(); while (enumerator.moveNext()) { value = enumerator.current(); if (setTwo.in(value)) { setTwo.remove(value); } } |
Best Regards,
Hossein Karimi
No comments:
Post a Comment