X++ Sample Collection Code
Collection classes were formerly called Foundation classes.
Class | Description | Data types | Access |
List | Values are stored sequentially with the ability to add values at the beginning or end of the list | All values must be the same data type | Use ListIterator or ListEnumerator |
Set | Values are stored non-sequentially. Duplicates are not added to the set. | All values must be the same data type | Use SetIterator or SetEnumerator |
Map | Values are stored by using a unique key. | The key and the value need not be from the same data type. | Use MapIterator or MapEnumerator |
Struct | Values are stored by using a unique string as a key | The key must be a string. The values can be of any type. | Provide the string key to the value method |
internal final class CollectionJob
{
/// <summary>
/// Class entry point. The system will call this method when a designated menu
/// is selected when execution starts and this class is set as the startup class.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
void printSET (Set setValue)
{
SetEnumerator _setEnumerator;
_setEnumerator = setValue.getEnumerator();
while(_setEnumerator.moveNext())
{
info(strFmt("%1",_setEnumerator.current()));
}
}
//Sample List
List sampleList = new List(Types::AnyType);
sampleList.addEnd(1);
sampleList.addEnd(2);
sampleList.addStart(3);
sampleList.addEnd(4);
ListEnumerator _listenumerator ;
_listenumerator = sampleList.getEnumerator();
info("----------------LIST START--------------------------");
while(_listenumerator.moveNext())
{
info(strFmt("%1",_listenumerator.current()));
}
info("----------------LIST END--------------------------");
//Sample Set
Set sampleSet1 = new Set(Types::AnyType);
Set sampleSet2 = new Set(Types::AnyType);
sampleSet1.add(1);
sampleSet1.add(2);
sampleSet1.add(3);
sampleSet2.add(2);
sampleSet2.add(1);
sampleSet2.add(4);
info("----------------SET START--------------------------");
printSET(sampleSet1);
printSET(sampleSet2);
printSET(Set::difference(sampleSet1,sampleSet2));
printSET(Set::union(sampleSet1,sampleSet2));
printSET(Set::intersection(sampleSet1,sampleSet2));
info("----------------SET END--------------------------");
//Sample Map
Map sampleMap = new Map(Types::AnyType,Types::AnyType);
sampleMap.insert(1,"A");
sampleMap.insert(3,"C");
sampleMap.insert(4,"D");
sampleMap.insert(2,"B");
sampleMap.insert(6,"F");
sampleMap.insert(5,"E");
MapEnumerator _mapEnumerator ;
_mapEnumerator = sampleMap.getEnumerator();
while(_mapEnumerator.moveNext())
{
info(" KEY %1",_mapEnumerator.currentKey());
info(" KEY %1",_mapEnumerator.currentValue());
}
info(strFmt("Does 4 exist? %1",sampleMap.exists(4)));
info(strFmt("Does E exist? %1",sampleMap.exists(5)));
sampleMap.lookup(6);
sampleMap.lookup(1);
//Sample Struct
container contaioner;
Struct unZipStruct;
Struct sampleStruct = new Struct(Types::AnyType,Types::AnyType);
sampleStruct.value("Employee FName","Muthu");
sampleStruct.value("Employee LName","Pandi");
sampleStruct.value("Employee Age",25);
sampleStruct.Add("Employee Exp",10);
contaioner = sampleStruct.pack();
unZipStruct = Struct::create(contaioner);
// Prints the type and name of all items in the struct
for (int i = 1; i <= sampleStruct.fields();i++)
{
print sampleStruct.fieldType(i), " ", sampleStruct.fieldName(i);
}
// Prints the type and name of all items in the struct
for (int i = 1; i <= unZipStruct.fields();i++)
{
print unZipStruct.fieldType(i), " ", unZipStruct.fieldName(i);
}
}
}
Comments
Post a Comment