[TOC]
# Array<T>
> class
no package
extended by [RegExpMatch](http://#)
* * * * *
所有平臺可用
* * * * *
一個 Array 是值的一個存儲形式。可以使用索引或者它的API訪問。
查看:
* http://haxe.org/manual/std-Array.html
* http://haxe.org/manual/lf-array-comprehension.html
## 構造函數
~~~
new ()
~~~
創建一個新的數組。
## 變量
### `length:Int`
* * * * *
只讀。
`this` 數組的長度。
## 方法
### `concat (a:Array<T>):Array<T>`
* * * * *
通過附加 `a` 的元素到 `this` Array 的元素返回一個新的數組。
這個操作不修改 `this` Array。
如果 `a` 是空的數組 `[]` ,返回 `this` Array 的一個拷貝。
返回數組的長度等于 `this.length` 和 `a.length` 的和。
如果 `a` 是 `null` ,結果則是未指定的。
### `copy ():Array<T>`
* * * * *
返回 `this` Array 的一個淺的拷貝。
元素不被拷貝,保留它們的身份,所以,對于任何有效的 `i` , `a[i]==a.copy()[i]` 是 `true`。然而,`a == a.copy()` 總是 `false` 。
### `filter (f:T ?> Bool):Array<T>`
* * * * *
返回一個數組,包含 this Array 中那些 `f` 返回 `true` 的元素。
個體的元素并不被賦值,保留它們的身份。
如果 f 為 null ,結果則是未指定的。
### `indexOf (x:T, ?fromIndex:Int):Int`
* * * * *
返回 `x` 在 `this` Array 中第一次出現的位置,從前往后搜索。
如果 `x` 通過檢查標準相等被發現,函數返回它的索引。
如果 `x` 沒有找到,函數返回 `-1` 。
如果 `fromIndex` 被指定,將會用作搜索開始的索引,否則從 `0` 索引開始搜索。如果是負數,它將作為從 `this` Array 末尾的偏移量來計算開始索引。 如果給定的或者計算的開始索引小于 `0` ,整個數組都會被搜索,如果大于或者等于 `this` Array 的長度,函數返回 `-1` 。
### `insert (pos:Int, x:T):Void`
* * * * *
插入元素 `x` 到 `pos` 位置。
這個操作修改 `this` Array。
偏移這樣計算:
如果 `pos` 超出 `this.length` ,偏移就是 `this.length`
如果 `pos` 是負數,偏移是從 `this` Array 的末尾計算,即,`this.length + pos` 。如果這產生一個負數,偏移為 `0` 。
否則,偏移就是 `pos` 。
如果結果的偏移沒有超過 `this.length` ,所有的元素從這個偏移開始(包括這個偏移)到 `this` Array 的結尾都會向前移動一個索引。
### `iterator ():Iterator<T>`
* * * * *
返回一個這個數組值的迭代器。
### `join (sep:String):String`
* * * * *
返回 `this` Array 的一個 字符串表示,使用 `sep` 分隔每個元素。
這個操作的結果等于 `Std.string(this[0]) + sep + Std.string(this[1]) + sep + ... + sep + Std.string(this[this.length-1])` 。
如果 `this` 是一個空數組 `[]` ,結果是空字符串 `""` 。如果 `this` 剛好有一個元素,結果等于調用 `Std.string(this[0])` 。
如果 `sep` 是 `null` ,結果則是未指定的。
### `lastIndexOf (x:T, ?fromIndex:Int):Int`
* * * * *
返回 `x` 在 `this` Array 中最后出現的位置,從后往前搜索。
如果 `x` 通過檢查標準相等被發現,函數返回它的索引。
如果 `x` 沒有被找到,函數返回 `-1` 。
如果 `fromIndex` 被指定,它將用作搜索的開始位置,否則搜索從最后一個元素索引開始。如果它是負數,將作為 從 `this` Array 尾部的偏移量來計算開始索引。如果給定或者計算的開始索引大于等于 `this` Array 的長度,整個數組會被搜索,如果它小于 `0` ,函數返回 `-1` 。
### `map<S> (f:T ?> S):Array<S>`
* * * * *
通過應用函數 `f` 到 `this` 的每個元素創建一個新的數組。
元素的順序將被保持。
如果 `f` 是 `null` ,結果則是未指定的。
### `pop ():Null<T>`
* * * * *
移除 `this` Array 的最后一個元素并返回它。
這個操作修改 `this` Array 。
如果 `this` 有至少一個元素,`this.length` 會減少 `1` 。
如果 `this` 是空數組 `[]` , `null` 被返回,長度仍然保持 `0` 。
### `push (x:T):Int`
* * * * *
添加元素 `x` 到 `this` Array 的末尾,并返回 `this` Array 新的 長度。
這個操作修改 `this` Array 。
`this.length` 增加 `1` 。
### `remove (x:T):Bool`
* * * * *
移除 `this` Array 中第一次出現的 `x` 。
這個操作修改 `this` Array 。
如果 `x` 通過檢查標準相等被發現,它被從 `this` Array 中移除,所有后面的元素被重新索引。然后函數返回 `true` 。
如果 `x` 沒有被找到, `this` Array 不會改變,函數返回 `false` 。
### `reverse ():Void`
* * * * *
反轉 `this` Array 中元素的順序。
這個操作修改 `this` Array 。
如果 `this.length < 2` , `this` 不會被改變。
### `shift ():Null<T>`
* * * * *
移除 `this` Array 的第一個元素并返回它。
這個操作修改 `this` Array 。
如果 `this` 有至少一個元素,`this.length` 和 每個剩下的元素的索引降低 `1` 。
如果 `this` 是空數組 `[]` ,`null` 被返回,長度保持為 `0` 。
### `slice (pos:Int, ?end:Int):Array<T>`
* * * * *
創建一個 `this` Array 某個范圍的淺拷貝,從 `pos` 開始,包含 `pos` 索引,到 `end` 位置,但是不包含 `end` 索引。
這個操作不修改 `this` Array 。
元素不會被拷貝,保留它們的身份。
如果 `end` 省略,或者超出` this.length` ,默認為 `this` Array 的結尾。
如果 `pos` 或 `end` 是負數,它們的偏移從 `this` Array 的末尾,分別被 `this.length + pos` 和 `this.length + end` 計算。如果這產生一個負數值,被 `0` 替代。
如果 `pos` 超出 `this.length`, 或者, 如果 `end` 超出或者等于 `pos`,結果則為 `[]` 。
### `sort (f:T ?> T ?> Int):Void`
* * * * *
根據比較函數 `f` 排序 `this` Array ,如果 `x==y` ,`f(x,y)` 返回 `0` ;如果 `x>y` ,返回一個正整數;如果 `x<y` ,返回一個負整數。
這個操作修改 `this` Array 。
排序操作不保證是穩定的,也就是說,相等的元素的順序可能被保留。對于一個穩定 Array 排序算法,`haxe.ds.ArraySort.sort()` 可以用來替代它。
如果 `f` 是 `null`, 結果則是未指定的。
### `splice (pos:Int, len:Int):Array<T>`
* * * * *
從 `this` Array 移除 `len` 長度的元素,從 `pos` 位置開始,包括 `pos` 索引,然后返回它們。
這個操作修改 `this` Array 。
如果 `len` 小于 `0`,或者 `pos` 超出 `this.length`, 結果為 空數組 `[]` 。
如果 `pos` 是負數,它的值從 `this` Array的末尾,通過 `this.length + pos` 計算。如果這產生一個負數值,`0` 用來替代它。
如果 `len` 和 `pos` 的和結果超過了 `this.length`, 這個操作會影響從 `pos` 到 `this` Array 末尾的元素。
返回的數組的長度等于 `this` Array 原來的長度減去 `this` Array 新的長度。換句話說,原來 `this` Array 中的每個元素要么保留在 `this` Array中,要么成為返回的數組中的一個元素。
### `toString ():String`
* * * * *
返回 `this` Array 的字符串表現。
結果會包括每個個體元素的字符串表現,并用逗號 , 分隔。閉合的 `[]` 在某些平臺會丟失,使用 `Std.string()` 來獲得一個字符串表現,可以保持平臺間的一致性。
### `unshift (x:T):Void`
* * * * *
添加 `x` 元素到 `this` Array 的開始。
這個操作修改 `this` Array 。
`this.length` 和 `this` Array 中的每個元素索引加 `1` 。