Arrays
Provides support for creation of arrays of any data type.
Array Constructors:
Array();
Array( nSize );
Array( element1, element2, ..., elementN);
Array Properties:
- nSize: The number of elements in the given array object (optional).
Array Examples:
aMyArray = new Array();
aMyArray = new Array( 20 );
aMyArray = new Array( 1,5,2,20,99 );
aMyArray = new Array( "Mon", "Tue", "Wed", "Thu", "Fri" );
Array Usage:
//Adding elements to an array for (x = 0; x < MyCount; x++) { aMyArray[x] = close() / open(); } //Retrieving elements from an array for (x = 0; x < MyCount; x++) { nMyValue = aMyArray[x]; debugPrint("Value: " + nMyValue + "\n"); }
Array Methods:
concat(array2) Concatenates array2 into the array object.
Array a = new Array(1,2,3);
Array b = new Array(4,5,6);
a = a.concat(b);
Contents of a is now: { 1, 2, 3, 4, 5, 6 }
join(sSeparator) Returns a string consisting of all the elements concatenated together.
Array a = new Array(1,2,3);
var s = a.join(",");
/* s = "1,2,3" */
length Returns the number of elements in the array.
Array a = new Array(1,2,3,9,2);
var len = a.length;
/* length == 5 */
pop() Removes the last element from the array and returns that element. This method changes the length of the array.
Array a = new Array(1,2,3);
var s = a.pop();
/* s = "3" and Array a now contains (1,2)*/
push() Adds one or more element to the end of an array and returns the new length of the array. This method changes the length of the array.
Array a = new Array(1,2,3);
var s = a.push(6,7);
/* s = "7" and Array a now contains (1,2,3,6,7)*/
shift() Removes the first element from an array and returns that element. This method changes the length of the array.
Array a = new Array(1,2,3);
var s = a.shift();
/* s = "1" and Array a now contains (2,3)*/
unshift() Adds one or more elements to the beginning of the array and returns the new length of the array.
Array a = new Array(1,2,3);
var s = a.unshift(0);
/* s = "4" and Array a now contains (0,1,2,3)*/
reverse() Reverses all the objects in the array object.
Array a = new Array(1,2,3,4,5);
/* Before Reverse, { 1,2,3,4,5} */
a.reverse();
/* After Reverse, { 5,4,3,2,1 } */
slice(nStartIndex, [nEndIndex]) Returns a section of the array from nStartIndex to (optionally) nEndIndex.
Array src = new Array(1,2,3,4,5,6);
Array dest = src.slice(2,4);
/* Contents of dest: { 3,4,5 } */
splice(nStartIndex, Quantity, [item1, ..., itemN]) Changes the contents of an array, adding new elements while removing old elements.
Array src = new Array(1,2,3,4,5,6);
var s = src.splice(2, 0, 9);
/* Contents of src: {1,2,9,3,4,5,6);
sort(sortFunction) Sorts the array using sortFunction
Array a = new Array(2,4,6,1,3,5);
a.sort();
/* Contents of a: { 1,2,3,4,5,6 } */
If you supply a sort function, it must be defined as:
function mySortFunction(firstarg, secondarg)
Return a negative value if firstarg < secondarg
Return a positive value if firstarg > secondarg.
Otherwise return 0.