Array

Description

A dynamic array of Variants.

Initialize an Array of 5 elements:

Array myArray = Array(5);

Arrays in FlexSim are 1-based, meaning the first element in the array is accessed via index 1.

double myVal = myArray[1]; // 1st element
myArray[2] = 5; // 2nd element
myArray[4] = 3.2; // 4th element

Since the Array is an array of Variants, you can set each element to anything that a Variant can hold, namely a number, string, treenode, or Array.

myArray[2] = "Hello World";
myArray[3] = 5.7;
myArray[4] = Model.find("Source1");
myArray[5] = Array(10); // now you can access myArray[5][2]

You can also fill an array with an initializer list.

Array myArray = [4.5, "Hello World", model(), ["Another", "Array"]];

By default, Arrays are passed around as shared references. This means if you set one Array variable to another Array variable, you are setting the variable to a reference to the first Array, not a copy of the first Array. Thus, if you change either variable's referenced Array data, and it will change it for both variables.

Array array1 = ["Hello", "World"];
Array array2 = array1;
array2[1] = "Goodbye"; // both array1 and array2 are ["Goodbye", "World"]

To do a deep copy of an Array, use the clone() method.

Array array1 = ["Hello", "World"];
Array array2 = array1.clone();
array2[1] = "Goodbye"; // array2 is ["Goodbye", "World"], array1 is unchanged.

Properties

length The number of elements in the array.

Methods

append Appends another array to the end of the array.
clone Creates a copy of the array.
concat Creates a new array by appending another array to the end of the array.
fill Fills the array with a value.
indexOf Searches the array for an element and returns its index.
join Makes a string by concatenating each element.
pop Removes and returns the last element of an array.
push Adds a new element to the end of the array.
reverse Reverses the order of the elements in the array.
shift Removes and returns the first element of an array.
slice Copies part of the array.
sort Sorts the elements in the array.
splice Adds and/or removes elements from an array.
unshift Adds a new element to the start of the array.

Constructor

Array Creates a new array of the given size.

Operators

!= Compares if the array is not equal to another array.
[] Gets or sets an element.
== Compares the array to another array.

Details

Do no remove, this fixes the anchor on doc.flexsim.com

Array.length

int length

Description

The number of elements in the array.

int numElements = myArray.length;

Can also be used to set the size of the array.


Array myArray = [1,2,3,4,5];
myArray.length = 3;  // [1,2,3]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.append()

Array append( Array otherArray )

Parameters

otherArray The array to append to this array.

Returns

Array The modified array.

Description

Appends another array to the end of the array.


Array myArray = [1,2,3,4,5];
myArray.append([7,8,9]);   //[1,2,3,4,5,7,8,9]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.clone()

Array clone( )

Returns

Array A copy of the array.

Description

Creates a copy of the array.


Array myArray = [1,2,3,4,5];
Array copy = myArray.clone();
myArray.push(6);

// myArray - [1,2,3,4,5,6]
// copy - [1,2,3,4,5]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.concat()

Array concat( Array otherArray )

Parameters

otherArray The array to concatenate with this array.

Returns

Array A new array made by concatenating the arrays.

Description

Creates a new array by appending another array to the end of the array.


Array concatArray = myArray.concat([7,8,9]);

// myArray - [1,2,3,4,5]
// concatArray - [1,2,3,4,5,7,8,9]
      

Similar to the append method except that the append method modifies the original array, whereas concat makes a new array.

Do no remove, this fixes the anchor on doc.flexsim.com

Array.fill()

Array fill( Variant val )

Parameters

val The value each element will now hold.

Returns

Array The modified array.

Description

Fills the array with a value.

This code sets each element to 0.


Array myArray = [1,2,3,4,5];
myArray.fill(0);  // [0,0,0,0,0]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.indexOf()

int indexOf( Variant val )

Parameters

val The element to look for.

Returns

int The index of the first occurence of the element in the array.

Description

Searches the array for an element and returns its index.

Returns -1 if the element is not found in the array.

Array myArray = ["Apple","Orange","Mango","Pear"];
int index = myArray.indexOf("Mango");  // 3
int index = myArray.indexOf(2); // -1
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.join()

string join( string separator = 0 )

Parameters

separator A string value that will be placed between elements when concatenating.

Returns

string The string of concatenated elements separated by the separator.

Description

Makes a string by concatenating each element.


Array myArray = [1,2,3,4,5];
string joined = myArray.join("-");  // "1-2-3-4-5"
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.pop()

Variant pop( )

Returns

Variant The last element of the array.

Description

Removes and returns the last element of an array.


Array myArray = [1,2,3,4,5];
Variant lastVal = myArray.pop();  // 5

//myArray - [1,2,3,4]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.push()

Array push( Variant val )

Parameters

val The new array element.

Returns

Array The modified array.

Description

Adds a new element to the end of the array.


Array myArray = [1,2,3,4,5];
myArray.push(6);  // [1,2,3,4,5,6]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.reverse()

Array reverse( )

Returns

Array The modified array.

Description

Reverses the order of the elements in the array.


Array myArray = [1,2,3,4,5];
myArray.reverse();  // [5,4,3,2,1]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.shift()

Variant shift( )

Returns

Variant The first element of the array.

Description

Removes and returns the first element of an array.


Array myArray = [1,2,3,4,5];
Variant firstVal = myArray.shift();  // 1

//myArray - [2,3,4,5]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.slice()

Array slice( int fromIndex , int toIndex )

Parameters

fromIndex The index of the first element in the selection.
toIndex The index of the element after the last element in the selection.

Returns

Array A copy of part of the array.

Description

Copies part of the array.


Array myArray = [1,2,3,4,5];
Array copy = myArray.slice(2, 5);  // [2,3,4]

//myArray - [1,2,3,4,5]
      

Selects the elements starting at fromIndex, and ends at, but does not include, toIndex.

Do no remove, this fixes the anchor on doc.flexsim.com

Array.sort()

Array sort( )

Returns

Array The modified array.

Description

Sorts the elements in the array.


Array myArray = [1,3,5,2,4];
myArray.sort();  // [1,2,3,4,5]

Array myArray2 = ["a","c","e","b","d"];
myArray2.sort();  // ["a","b","c","d","e"]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.splice()

Array splice( int fromIndex , int count )
Array splice( int fromIndex , int count , Array insert )

Parameters

fromIndex The index where elements will be removed and then added.
count The number of elements to remove.
insert An array of elements to be added at the fromIndex.

Returns

Array The modified array.

Description

Adds and/or removes elements from an array.


Array cars = ["Volvo", "BMW", "Audi", "Ford"];
cars.splice(2, 2); 			// ["Volvo", "Ford"]
cars.splice(3, 1, ["Lotus", "Kia"]);  	// ["Volvo", "BMW", "Lotus", "Kia", "Ford"]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.unshift()

Array unshift( Variant val )

Parameters

val The new array element.

Returns

Array The modified array.

Description

Adds a new element to the start of the array.


Array myArray = [1,2,3,4,5];
myArray.unshift(6);  // [6,1,2,3,4,5]
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array Constructor

Array( int size )

Parameters

size The size of the new array.

Description

Creates a new array of the given size.

Array myArray = Array(5); // [,,,,]
Do no remove, this fixes the anchor on doc.flexsim.com

Array.operator !=

int operator !=( Array other )

Parameters

other The other array to compare this array to.

Returns

int False (0) if all elements of both arrays are the same. True (1) otherwise.

Description

Compares if the array is not equal to another array.

Array myArray = [1,2,3,4,5];
Array otherArray1 = [1,2,3,4,5];
Array otherArray2 = [1,2,4,5];
myArray != otherArray1; // false
myArray != otherArray2; // true
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.operator []

Variant operator []( int index )

Parameters

index The index of an element

Returns

Variant An element of the array.

Description

Gets or sets an element.


Array myArray = [1,2,3,4,5];
myArray[2] = 6;              // [1,6,3,4,5]
Variant value = myArray[3];  // 3
      
Do no remove, this fixes the anchor on doc.flexsim.com

Array.operator ==

int operator ==( Array other )

Parameters

other The other array to compare this array to.

Returns

int True (1) if all elements of both arrays are the same. False (0) otherwise.

Description

Compares the array to another array.

Array myArray = [1,2,3,4,5];
Array otherArray1 = [1,2,3,4,5];
Array otherArray2 = [1,2,4,5];
myArray == otherArray1; // true
myArray == otherArray2; // false