Array

You are viewing an old version of this entry, click here to see latest version.
class Array<T>Available in flash8, flash, neko, js, php, cppAn Array is a storage for values. You can access it using indexes or with its API. On the server side, it's often better to use a "List" which is less memory and CPU consuming, unless you really need indexed access. var length(default,null) : IntThe length of the Array. (Read Only) function new() : VoidCreates a new Array. function concat( a : Array<T> ) : Array<T>Returns a new Array by appending a to this. function copy() : Array<T>Returns a copy of the Array. The values are not copied, only the Array structure. function insert( pos : Int, x : T ) : VoidInserts the element x at the position pos. All elements after pos are moved one index ahead. If "pos" is bigger than the array length element "x" will be inserted at position "pos" and default values will fill in-between. (Default values are null on 'dynamic' platforms, actual values on 'static' platforms. So Array<Int> empirically has 0 as the default value on hxcpp.) function iterator() : Iterator<Null<T>>Returns an iterator of the Array values. function join( sep : String ) : StringReturns a representation of an array with sep for separating each element. function pop() : Null<T>Removes the last element of the array and returns it. function push( x : T ) : IntAdds the element x at the end of the array. Returns the new length of the array. function remove( x : T ) : BoolRemoves the first occurence of x. Returns false if x was not present. Elements are compared by using standard equality. function reverse() : VoidReverse the order of elements of the Array. function shift() : Null<T>Removes the first element and returns it. function slice( pos : Int, ?end : Int ) : Array<T>Copies the range of the array starting at pos up to, but not including, end. Both pos and end can be negative to count from the end: -1 is the last item in the array.
function sort( f : T -> T -> Int ) : Void

Sort the Array according to the comparison function f.

f(x,y) should return:

  • 0 if x == y
  • >0 if x should appear after y
  • <0 if x should appear before y

function splice( pos : Int, len : Int ) : Array<T>Removes len elements starting from pos an returns them. function toString() : StringReturns a displayable representation of the Array content. function unshift( x : T ) : VoidAdds the element x at the start of the array.
version #14303, modified 2012-06-22 22:59:46 by foobar
3 comments
  • Aug 29, 2012 at 23:53

    Clarified definition of array to include the stuff about Haxe's implementation of Array that's important to people migrating from other languages: that it can't have gaps, that it starts at 0, and it's only indexed by integers.
    Also added cross-refs to various other data-structures that might be what they are looking for if they came from another language.
    A Wiki (or any programming docs) with few cross-refs is a very sad thing: I hope these edits made it happier.

  • Feb 05, 2013 at 01:05

    If you are wondering if array[big#]=42 will work, it will; see the note at the top about there being no "gaps" in arrays.

  • amn
    May 11, 2013 at 19:10

    In AVM2 (ActionScript Virtual Machine, as used by Flash Player running SWF movies tagged as version 9 or later) Array.length is WRITABLE property. In haXe, it is read-only, and so if you want to truncate an array in AVM2, in haXe you would have to resort to the "untyped":

    untyped a.length = 0;