ListIterator in X++ Programming Language

X++ Programming Fundamentals - D365 F&O Technical (Article) (Program)

10

Given Input:

Create a list to store integers using Types::Integer.

Expected Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10

Program:

internal final class ListExampleDemo
{
   /// <summary>
   /// Entry point of the program. 
   /// Demonstrates how to use a List and ListIterator in X++
   /// </summary>
   /// <param name="_args">Runtime arguments passed to the program.</param>
   public static void main(Args _args)
   {
       // Step 1: Initialize a list to store integers
       List integerList = new List(Types::Integer); 
       ListIterator listIterator; 
       int currentNumber;

       // Step 2: Add numbers from 1 to 10 to the list
       for (currentNumber = 1; currentNumber <= 10; currentNumber++) 
       { 
           integerList.addEnd(currentNumber); 
       }

       // Step 3: Initialize an iterator to traverse the list
       listIterator = new ListIterator(integerList); 

       // Step 4: Traverse and print each number in the list
       while (listIterator.more()) 
       { 
           int numberToPrint = listIterator.value(); // Get the current value
           print strFmt("Number: %1", numberToPrint); // Print the value
           listIterator.next(); // Move to the next element
       }

       // Step 5: Pause to allow the user to view the output
       pause;
   }
}

Output: