Home› D365 F&O Technical Using X++ Programming Language› / Programs / ListIterator in X++ Programming Language
🚀 Programming Example

ListIterator in X++ Programming Language

64 Views
Beginner
Code Snippet
Learn this program step-by-step with algorithm, source code, output and detailed explanation.
No previous program
No next program

📌 Information & Algorithm

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 Code

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;
   }
}
No previous program
No next program
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.