Lists in X++ (D365 FnO)

List is a type of data structure and collections, it can contain unlimited items, in X++, list can be created of several Types(specified in Types base enum), the type must be specified on the declaration and it cannot be changed after the initialization.

There are some classes exists to enumerated and iterate the list object. ListIterator object has methods that can insert and delete items from list. ListEnumeration cannot modify the list content but can traverse the same.


   List      myList    = new List(Types::Integer);  
   List      myListString = new List(Types::String);  
   ListIterator  literator;
  
   // add the element at the end of the list  
   myList.addEnd(200);   

   // add the element at the start of the list  
   myList.addStart(150);  
 
   myList.addEnd(70);   
   myListString.addEnd ("Second");  
   myListString.addStart ("First"); 

  
   // If you want to insert the data at some specific index, then you need to make use of the listIterator class   
   // Iterator performs a midpoint   
   // insert at current position.  
   literator = new ListIterator(myListString);  

   while (literator.more())  
   {  
     // can provide some condition, i.e. if check etc  
     if (literator.value() == "First")  
     {  
       listIterator.insert ("Between first and second");  
     }  
   }   

After making changes in the list using the ListIterator, you can use the same list which was used to initialise the ListIterator class and it will be updated with current values.


Comments