Jaa


Arrays

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

An array is a collection of variables that are all of the same type. The elements of an array are accessed with simple integer indexes. For example:

int myArray[10]; // Fixed-length array with 10 integers

myArray[4] = 1; // Accessing the 4th element in the array

You use a separate statement to initialize each element in an array.

Note

When you use a Container data type or an Array object to create a collection, you can initialize multiple elements by using a single statement.

There are three kinds of arrays:

  • Dynamic

  • Fixed-length

  • Partly on disk

X++ only supports one-dimensional arrays. It is possible, however, to mimic the behavior of multiple array indices.

Note

Variables in objects and tables can be declared as arrays (this is used in address lines in the standard application).

An Array collection class enables you to store objects in an array.

Declaring Arrays

Array declaration

=

datatype Variable { , Variable } ;

Variable

=

Identifier arrayoptions

Arrayoptions

=

[ [ Length ] [, Memory ] ]

If a Length is specified, the array is a fixed-length array with Length elements. Otherwise, it is a dynamic array.

If Memory is specified, it is a partly on disk array.

    static void Job1(Args _args)
    {
        // A dynamic array of integers
        int i[]; 
     
        // A fixed-length real array with 100 elements
        real r[100]; 
     
        // A dynamic array of dates with only 10 elements in memory
        date d[,10]; 
     
        // A fixed length array of NoYes variables with
        // 100 elements and 10 in memory
        NoYes ny[100,10];
        
        print "Done.";
        pause;
    }

Array Indices

Array indexes begin at 1. The first item in the array is called [1], the second [2], and so on.

The syntax for accessing an array element is:

ArrayItemReference = ArrayVariable [ Index ]

where ArrayVariable is the identifier of the array, and Index is the number of the array element. Index can be an integer expression.

For example, a[9] accesses item number 9 in array a.

In X++, item zero[0] is used to clear the array! Assigning a value to index 0 in an array resets all elements in the array to the default value. For example,

intArray[0] = 0; //Resets all elements in intArray

Dynamic Arrays

A dynamic array is declared with an empty array option (that is, only square brackets):

//Dynamic array of integers

int intArray[];

// Dynamic array of variables of type Datatype

Datatype arrayVariable[];

Fixed-length Arrays

A fixed-length array can hold the number of items that is specified in the declaration. Fixed-length arrays are declared like dynamic arrays but with a length option in the square brackets:

boolean boolArray[100]; //Fixed-length array of booleans with 100 items

Partly On Disk Arrays

Partly on disk arrays are declared either as dynamic or fixed-length arrays with an extra option that declares how many items should be held in memory. The other items are stored on disk and automatically loaded when referenced.

//Dynamic integer array with only 100 elements in memory. 
 int arrayVariable [ ,100];

//Fixed-length string array with 1000 elements, and only 200 in memory. 
 str arrayVariable [1000,200]

See also

Composite Data Types

Multiple Array Indexes

Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.