パッケージトップレベル
public dynamic class Array
継承Array Inheritance Object

Array クラスを使用すると、配列にアクセスして操作することができます。配列インデックスは 0 から始まります。つまり、配列内の最初のエレメントは [0]、2 番目のエレメントは [1] (以下同様) になります。Array オブジェクトを作成するには、new Array() コンストラクタを使用します。Array() は関数としても呼び出すことができます。さらに、配列アクセス ([]) 演算子を使用すると、配列の初期化または配列のエレメントへのアクセスを行うことができます。

配列エレメントには、数値、ストリング、オブジェクトを含む各種データ型、さらに他の配列をも保存できます。 多次元 配列は、インデックス付き配列を作成し、その各エレメントに異なるインデックス配列を割り当てることで作成できます。このような配列は、テーブル内のデータの表現に使用できるので、多次元であると考えられます。

配列は希薄な配列です。つまり、インデックス 0 に 1 つのエレメント、インデックス 5 に別のエレメントが存在しても、この 2 つのエレメントの間のインデックス位置にはエレメントが存在しない場合があります。このような場合、1 から 4 までの位置のエレメントは未定義となります。これはエレメントが存在しないことを示しており、必ずしも値 undefined を持つエレメントが存在するわけではありません。

配列では、値による割り当てではなく、参照による割り当てが行われます。ある配列変数に別の配列変数を割り当てると、両方とも同じ配列を参照するようになります。

 var oneArray:Array = new Array("a", "b", "c");
 var twoArray:Array = oneArray; // Both array variables refer to the same array.
 twoArray[0] = "z";             
 trace(oneArray);               // Output: z,b,c.
 

結合配列 (ハッシュとも呼ばれる) の作成には Array クラスを使用しないでください。結合配列は、数値エレメントではなく名前付きエレメントを含むデータ構造です。結合配列の作成には、Object クラスを使用してください。ActionScript では Array クラスを使用して結合配列を作成できますが、結合配列で Array クラスのメソッドやプロパティは使用できません。

Array クラスを拡張してメソッドをオーバーライドまたは追加することはできます。ただし、サブクラスを dynamic として指定する必要があります。そうしない場合、配列にデータを保管する機能が失われます。

例の表示

関連項目

[] (array access)
Object class
関数のパラメータ


パブリックプロパティ
 プロパティ定義
 Inheritedconstructor : Object
指定されたオブジェクトインスタンスのクラスオブジェクトまたはコンストラクタ関数への参照です。
Object
  length : uint
配列内のエレメント数を示す負でない整数です。
Array
 Inheritedprototype : Object
[static] クラスまたは関数オブジェクトのプロトタイプオブジェクトへの参照です。
Object
パブリック Methods
 メソッド定義
  
Array(numElements:int = 0)
指定された数のエレメントを持つ配列を作成することができます。
Array
  
Array(... values)
指定されたエレメントを持つ配列を作成することができます。
Array
  
concat(... args):Array
パラメータで指定されたエレメントを配列内のエレメントと連結して、新しい配列を作成します。
Array
  
every(callback:Function, thisObject:* = null):Boolean
指定された関数について false を返すアイテムに達するまで、配列内の各アイテムにテスト関数を実行します。
Array
  
filter(callback:Function, thisObject:* = null):Array
配列内の各アイテムについてテスト関数を実行し、指定された関数について true を返すすべてのアイテムを含む新しい配列を作成します。
Array
  
forEach(callback:Function, thisObject:* = null):void
配列内の各アイテムについて関数を実行します。
Array
 Inherited
指定されたプロパティがオブジェクトに定義されているかどうかを示します。
Object
  
indexOf(searchElement:*, fromIndex:int = 0):int
厳密な等価 (===) を使用して配列内のアイテムを検索し、アイテムのインデックス位置を返します。
Array
 Inherited
Object クラスのインスタンスが、パラメータとして指定されたオブジェクトのプロトタイプチェーン内にあるかどうかを示します。
Object
  
join(sep:*):String
配列内のエレメントをストリングに変換し、指定されたセパレータをエレメント間に挿入し、エレメントを連結して、その結果をストリングとして返します。
Array
  
lastIndexOf(searchElement:*, fromIndex:int = 0x7fffffff):int
配列内のアイテムを、最後のアイテムから先頭に向かって検索し、厳密な等価 (===) を使用して、一致したアイテムのインデックス位置を返します。
Array
  
map(callback:Function, thisObject:* = null):Array
配列内の各アイテムについて関数を実行し、元の配列の各アイテムに対する関数の結果に対応するアイテムから成る新しい配列を作成します。
Array
  
配列の最後のエレメントを削除して、そのエレメントの値を返します。
Array
 Inherited
指定されたプロパティが存在し、列挙できるかどうかを示します。
Object
  
push(... args):uint
エレメントを配列の最後に追加して、追加後の配列の長さを返します。
Array
  
配列の並びを反転させます。
Array
 Inherited
ループ処理に対するダイナミックプロパティの可用性を設定します。
Object
  
配列の最初のエレメントを削除して、そのエレメントを返します。
Array
  
slice(startIndex:int = 0, endIndex:int = 16777215):Array
元の配列から一連のエレメントを取り出して、新しい配列を返します。元の配列は変更されません。
Array
  
some(callback:Function, thisObject:* = null):Boolean
true を返すアイテムに達するまで、配列内の各アイテムにテスト関数を実行します。
Array
  
sort(... args):Array
配列内のエレメントをソートします。
Array
  
sortOn(fieldName:Object, options:Object = null):Array
配列内のフィールド (複数のフィールドも可能) に基づいて、配列内のエレメントをソートします。
Array
  
splice(startIndex:int, deleteCount:uint, ... values):Array
配列のエレメントを追加および削除します。
Array
  
指定された配列内のエレメントを表すストリングを返します。
Array
  
指定された配列内のエレメントを表すストリングを返します。
Array
  
unshift(... args):uint
エレメントを配列の先頭に追加して、配列の新しい長さを返します。
Array
 Inherited
指定されたオブジェクトのプリミティブな値を返します。
Object
パブリック定数
 定数定義
  CASEINSENSITIVE : uint = 1
[static] Array クラスのソートメソッドに対して、大文字と小文字を区別しないソートを指定します。
Array
  DESCENDING : uint = 2
[static] Array クラスのソートメソッドに対して、降順でのソートを指定します。
Array
  NUMERIC : uint = 16
[static] Array クラスのソートメソッドに対して、文字ストリングではなく数値によるソートを指定します。
Array
  RETURNINDEXEDARRAY : uint = 8
[static] ソート結果として、配列インデックスで構成される配列を返すことを指定します。
Array
  UNIQUESORT : uint = 4
[static] Array クラスのソートメソッドに対して、一意性ソート要件を指定します。
Array
プロパティの詳細
lengthプロパティ
length:uint  [read-write]

配列内のエレメント数を示す負でない整数です。このプロパティは、新しいエレメントが配列に追加されると自動更新されます。配列エレメントに値を割り当てるとき (my_array[index] = value など)、index が数値でかつ index+1length プロパティよりも大きい場合、length プロパティが index+1 に更新されます。

メモ : length プロパティに既存の長さより短い値を割り当てた場合、配列は切り詰められます。

実装
    public function get length():uint
    public function set length(value:uint):void


The following code creates an Array object names with the string element Bill. It then uses the push() method to add another string element Kyle. The length of the array, as determined by the length property, was one element before the use of push() and is two elements after push() is called. Another string, Jeff, is added to make the length of names three elements. The shift() method is then called twice to remove Bill and Kyle, making the final array of length one.

var names:Array = new Array("Bill"); names.push("Kyle"); trace(names.length); // 2

names.push("Jeff"); trace(names.length); // 3

names.shift(); names.shift(); trace(names.length); // 1

コンストラクタの詳細
Array()コンストラクタ
public 関数 Array(numElements:int = 0)

指定された数のエレメントを持つ配列を作成することができます。パラメータを指定しない場合は、0 個のエレメントを持つ配列が作成されます。エレメント数を指定する場合は、numElements 個のエレメントを持つ配列が作成されます。

メモ : このクラスは、2 つのコンストラクタメソッド項目を示しています。コンストラクタはさまざまなタイプの引数を受け入れるためです。コンストラクタの動作は、各項目で詳細を示すように、渡される引数の型と数によって異なります。ActionScript 3.0 はメソッドまたはコンストラクタのオーバーロードをサポートしていません。

パラメータ
numElements:int (default = 0) — 配列内のエレメント数を指定する整数です。

スロー
RangeError — 引数は、0 以上の整数ではない数値です。

関連項目



The following example creates the Array object myArr with no arguments and an initial length of 0:
 package { import flash.display.Sprite;

    public class Array_Array extends Sprite {

        public function Array_Array() { var myArr:Array = new Array(); trace(myArr.length); // 0 } } }

The following example creates an Array object with 5 initial elements, with a length of 5, and populates the first element with the string "one", and adds the string element "six" to the end of the array by using the push() method:
 package { import flash.display.Sprite;

    public class Array_Array_2 extends Sprite {

        public function Array_Array_2() { var myArr:Array = new Array(5); trace(myArr.length); // 5 myArr[0] = "one"; myArr.push("six"); trace(myArr);         // one,,,,,six trace(myArr.length); // 6 } } }

Array()コンストラクタ 
public 関数 Array(... values)

指定されたエレメントを持つ配列を作成することができます。任意の型の値を指定することができます。配列内の最初のエレメントのインデックスまたは位置は、常に 0 です。

メモ : このクラスは、2 つのコンストラクタ項目を示しています。コンストラクタはさまざまなタイプの引数を受け入れるためです。コンストラクタの動作は、各項目で詳細を示すように、渡される引数の型と数によって異なります。ActionScript 3.0 はメソッドまたはコンストラクタのオーバーロードをサポートしていません。

パラメータ
... values — 複数の任意の値で構成されるカンマ区切りリストです。

メモ : Array コンストラクタに数値パラメータを 1 つだけ渡した場合、そのパラメータは配列の length プロパティを指定するものと見なされます。


スロー
RangeError — 引数は、0 以上の整数ではない数値です。

関連項目



The following example creates a new Array object with an initial length of 3, populates the array with the string elements one, two, and three, and then converts the elements to a string.
 package { import flash.display.Sprite;

    public class Array_Array_3 extends Sprite {

        public function Array_Array_3() { var myArr:Array = new Array("one", "two", "three"); trace(myArr.length); // 3 trace(myArr);          // one,two,three } } }

メソッドの詳細
concat()メソッド
AS3 function concat(... args):Array

パラメータで指定されたエレメントを配列内のエレメントと連結して、新しい配列を作成します。パラメータで配列を指定すると、その配列のエレメントが連結されます。

パラメータ
... args — 新しい配列内で連結する任意のデータ型 (数値、エレメント、ストリングなど) の値です。値を渡さない場合、新しい配列は元の配列の複製になります。

戻り値
Array — この配列のエレメントの後にパラメータのエレメントが続く配列です。


The following code creates four Array objects:

var numbers:Array = new Array(1, 2, 3); var letters:Array = new Array("a", "b", "c"); var numbersAndLetters:Array = numbers.concat(letters); var lettersAndNumbers:Array = letters.concat(numbers);

trace(numbers);       // 1,2,3 trace(letters);       // a,b,c trace(numbersAndLetters); // 1,2,3,a,b,c trace(lettersAndNumbers); // a,b,c,1,2,3

every()メソッド 
AS3 function every(callback:Function, thisObject:* = null):Boolean

指定された関数について false を返すアイテムに達するまで、配列内の各アイテムにテスト関数を実行します。このメソッドを使用して、配列内のすべてのアイテムがある基準 (値が特定の数値より小さいなど) を満たしているかどうかを判別できます。

このメソッドで、2 番目のパラメータ thisObject は、最初のパラメータ callback がメソッド閉包である場合、null でなければなりません。たとえば、me というムービークリップ内に、ある関数を作成するとします。

     function myFunction(){
        //your code here
     }
     

次に、myArray という配列に対して filter() メソッドを使用するとします。

     myArray.filter(myFunction, me);
     

myFunction は Timeline クラスのメンバーであり、me によってオーバーライドできないため、Flash Player は例外をスローします。このランタイムエラーは、次のように関数を変数に割り当てることによって回避できます。

     var foo:Function = myFunction() {
         //your code here
     };
     myArray.filter(foo, me);
     

パラメータ
callback:Function — 配列内の各アイテムについて実行する関数です。この関数には、単純な比較 (item < 20 など) やより複雑な演算を含めることができます。また、この関数は、次のように、アイテムの値、アイテムのインデックス、および Array オブジェクトの 3 つの引数を使用して呼び出されます。
function callback(item:*, index:int, array:Array):Boolean;
 
thisObject:* (default = null) — 関数の this として使用するオブジェクトです。

戻り値
Boolean — ブール値です。指定された関数について配列内のすべてのアイテムが true を返す場合は true、それ以外の場合は false を返します。

関連項目



The following example tests two arrays to determine whether every item in each array is a number. It also outputs the results of the test, showing that isNumeric is true for the first array and false for the second:
 package { import flash.display.Sprite; public class Array_every extends Sprite { public function Array_every() { var arr1:Array = new Array(1, 2, 4); var res1:Boolean = arr1.every(isNumeric); trace("isNumeric:", res1); // true
 
            var arr2:Array = new Array(1, 2, "ham"); var res2:Boolean = arr2.every(isNumeric); trace("isNumeric:", res2); // false } private function isNumeric(element:*, index:int, arr:Array):Boolean { return (element is Number); } } }

filter()メソッド 
AS3 function filter(callback:Function, thisObject:* = null):Array

配列内の各アイテムについてテスト関数を実行し、指定された関数について true を返すすべてのアイテムを含む新しい配列を作成します。false を返すアイテムは、新しい配列には含められません。

このメソッドで、2 番目のパラメータ thisObject は、最初のパラメータ callback がメソッド閉包である場合、null でなければなりません。たとえば、me というムービークリップ内に、ある関数を作成するとします。

     function myFunction(){
        //your code here
     }
     

次に、myArray という配列に対して filter() メソッドを使用するとします。

     myArray.filter(myFunction, me);
     

myFunction は Timeline クラスのメンバーであり、me によってオーバーライドできないため、Flash Player は例外をスローします。このランタイムエラーは、次のように関数を変数に割り当てることによって回避できます。

     var foo:Function = myFunction() {
         //your code here
         };
     myArray.filter(foo, me);
     
パラメータ
callback:Function — 配列内の各アイテムについて実行する関数です。この関数には、単純な比較 (item < 20 など) やより複雑な演算を含めることができます。また、この関数は、次のように、アイテムの値、アイテムのインデックス、および Array オブジェクトの 3 つの引数を使用して呼び出されます。
    function callback(item:*, index:int, array:Array):Boolean;
 
thisObject:* (default = null) — 関数の this として使用するオブジェクトです。

戻り値
Array — 元の配列内のアイテムで true を返したものをすべて含む新しい配列です。

関連項目



The following example creates an array of all employees who are managers:
 package { import flash.display.Sprite; public class Array_filter extends Sprite { public function Array_filter() { var employees:Array = new Array(); employees.push({name:"Employee 1", manager:false}); employees.push({name:"Employee 2", manager:true}); employees.push({name:"Employee 3", manager:false}); trace("Employees:"); employees.forEach(traceEmployee);
            
            var managers:Array = employees.filter(isManager); trace("Managers:"); managers.forEach(traceEmployee); } private function isManager(element:*, index:int, arr:Array):Boolean { return (element.manager == true); } private function traceEmployee(element:*, index:int, arr:Array):void { trace("\t" + element.name + ((element.manager) ? " (manager)" : "")); } } }

forEach()メソッド 
AS3 function forEach(callback:Function, thisObject:* = null):void

配列内の各アイテムについて関数を実行します。

このメソッドで、2 番目のパラメータ thisObject は、最初のパラメータ callback がメソッド閉包である場合、null でなければなりません。たとえば、me というムービークリップ内に、ある関数を作成するとします。

     function myFunction(){
        //your code here
     }
     

次に、myArray という配列に対して filter() メソッドを使用するとします。

     myArray.filter(myFunction, me);
     

myFunction は Timeline クラスのメンバーであり、me によってオーバーライドできないため、Flash Player は例外をスローします。このランタイムエラーは、次のように関数を変数に割り当てることによって回避できます。

     var foo:Function = myFunction() {
         //your code here
         };
     myArray.filter(foo, me);
     
パラメータ
callback:Function — 配列内の各アイテムについて実行する関数です。この関数には、単純なコマンド (trace() ステートメントなど) やより複雑な演算を含めることができます。また、この関数は、次のように、アイテムの値、アイテムのインデックス、および Array オブジェクトの 3 つの引数を使用して呼び出されます。
    function callback(item:*, index:int, array:Array):void;
 
thisObject:* (default = null) — 関数の this として使用するオブジェクトです。


The following example runs the trace() statement in the traceEmployee() function on each item in the array:
 package { import flash.display.Sprite; public class Array_forEach extends Sprite { public function Array_forEach() { var employees:Array = new Array(); employees.push({name:"Employee 1", manager:false}); employees.push({name:"Employee 2", manager:true}); employees.push({name:"Employee 3", manager:false}); trace(employees); employees.forEach(traceEmployee); } private function traceEmployee(element:*, index:int, arr:Array):void { trace(element.name + " (" + element.manager + ")"); } } }

The following example also runs the trace() statement in a slightly altered traceEmployee() function on each item in the array:
 package { import flash.display.Sprite; public class Array_forEach_2 extends Sprite { public function Array_forEach_2() { var employeeXML:XML = <employees> <employee name="Steven" manager="false" /> <employee name="Bruce" manager="true" /> <employee name="Rob" manager="false" /> </employees>; var employeesList:XMLList = employeeXML.employee; var employeesArray:Array = new Array(); for each (var tempXML:XML in employeesList) { employeesArray.push(tempXML); } employeesArray.sortOn("@name"); employeesArray.forEach(traceEmployee); } private function traceEmployee(element:*, index:Number, arr:Array):void { trace(element.@name + ((element.@manager == "true") ? " (manager)" : "")); } } }

indexOf()メソッド 
AS3 function indexOf(searchElement:*, fromIndex:int = 0):int

厳密な等価 (===) を使用して配列内のアイテムを検索し、アイテムのインデックス位置を返します。

パラメータ
searchElement:* — 配列内で検索するアイテムです。
 
fromIndex:int (default = 0) — アイテムの検索を開始する配列内の場所です。

戻り値
int — 配列内のアイテムの 0 から始まるインデックス位置です。searchElement 引数が見つからなかった場合、戻り値は -1 です。

関連項目



The following example displays the position of the specified array:
 package { import flash.display.Sprite; public class Array_indexOf extends Sprite { public function Array_indexOf() { var arr:Array = new Array(123,45,6789); arr.push("123-45-6789"); arr.push("987-65-4321");
            
            var index:int = arr.indexOf("123"); trace(index); // -1
            
            var index2:int = arr.indexOf(123); trace(index2); // 0 } } }

join()メソッド 
AS3 function join(sep:*):String

配列内のエレメントをストリングに変換し、指定されたセパレータをエレメント間に挿入し、エレメントを連結して、その結果をストリングとして返します。ネストされた配列は、join() メソッドに渡されるセパレータで区切るのではなく、常にカンマ (,) で区切ります。

パラメータ
sep:* — 返されたストリング内の配列エレメントを区切る文字またはストリングです。このパラメータを省略すると、デフォルトのセパレータとしてカンマが使用されます。

戻り値
String — ストリングに変換された配列のエレメントで構成され、指定されたパラメータで区切られているストリングです。

関連項目



The following code creates an Array object myArr with elements one, two, and three and then a string containing one and two and three using the join() method.

var myArr:Array = new Array("one", "two", "three"); var myStr:String = myArr.join(" and "); trace(myArr); // one,two,three trace(myStr); // one and two and three

The following code creates an Array object specialChars with elements (, ), -, and a blank space and then creates a string containing (888) 867-5309. Then, using a for loop, it removes each type of special character listed in specialChars to produce a string (myStr) that contains only the digits of the phone number remaining: 888675309. Note that other characters, such as +, could have been added to specialChars and then this routine would work with international phone number formats.

var phoneString:String = "(888) 867-5309";

var specialChars:Array = new Array("(", ")", "-", " "); var myStr:String = phoneString;

var ln:uint = specialChars.length; for(var i:uint; i < ln; i++) { myStr = myStr.split(specialChars[i]).join(""); }

var phoneNumber:Number = new Number(myStr);

trace(phoneString); // (888) 867-5309 trace(phoneNumber); // 8888675309

lastIndexOf()メソッド 
AS3 function lastIndexOf(searchElement:*, fromIndex:int = 0x7fffffff):int

配列内のアイテムを、最後のアイテムから先頭に向かって検索し、厳密な等価 (===) を使用して、一致したアイテムのインデックス位置を返します。

パラメータ
searchElement:* — 配列内で検索するアイテムです。
 
fromIndex:int (default = 0x7fffffff) — アイテムの検索を開始する配列内の場所です。デフォルトは、インデックスに許可されている最大値です。fromIndex を指定しない場合、配列内の最後のアイテムから検索が開始されます。

戻り値
int — 配列内のアイテムの 0 から始まるインデックス位置です。searchElement 引数が見つからなかった場合、戻り値は -1 です。

関連項目



The following example displays the position of the specified array:
 package { import flash.display.Sprite; public class Array_lastIndexOf extends Sprite { public function Array_lastIndexOf() { var arr:Array = new Array(123,45,6789,123,984,323,123,32);
            
            var index:int = arr.indexOf(123); trace(index); // 0
            
            var index2:int = arr.lastIndexOf(123); trace(index2); // 6 } } }

map()メソッド 
AS3 function map(callback:Function, thisObject:* = null):Array

配列内の各アイテムについて関数を実行し、元の配列の各アイテムに対する関数の結果に対応するアイテムから成る新しい配列を作成します。

このメソッドで、2 番目のパラメータ thisObject は、最初のパラメータ callback がメソッド閉包である場合、null でなければなりません。たとえば、me というムービークリップ内に、ある関数を作成するとします。

     function myFunction(){
        //your code here
     }
     

次に、myArray という配列に対して filter() メソッドを使用するとします。

     myArray.filter(myFunction, me);
     

myFunction は Timeline クラスのメンバーであり、me によってオーバーライドできないため、Flash Player は例外をスローします。このランタイムエラーは、次のように関数を変数に割り当てることによって回避できます。

     var foo:Function = myFunction() {
         //your code here
         };
     myArray.filter(foo, me);
     
パラメータ
callback:Function — 配列内の各アイテムについて実行する関数です。この関数には、単純なコマンド (ストリングの配列の大文字小文字の変更など) やより複雑な演算を含めることができます。また、この関数は、次のように、アイテムの値、アイテムのインデックス、および Array オブジェクトの 3 つの引数を使用して呼び出されます。
    function callback(item:*, index:int, array:Array):void;
 
thisObject:* (default = null) — 関数の this として使用するオブジェクトです。

戻り値
Array — 元の配列内の各アイテムの関数の結果が含まれる新しい配列です。

関連項目



The following example changes all items in the array to use uppercase letters:
 package { import flash.display.Sprite; public class Array_map extends Sprite { public function Array_map() { var arr:Array = new Array("one", "two", "Three"); trace(arr); // one,two,Three

            var upperArr:Array = arr.map(toUpper); trace(upperArr); // ONE,TWO,THREE } private function toUpper(element:*, index:int, arr:Array):String { return String(element).toUpperCase(); } } }

pop()メソッド 
AS3 function pop():Object

配列の最後のエレメントを削除して、そのエレメントの値を返します。

戻り値
Object — 指定した配列の最後のエレメント (任意のデータ型) の値です。

関連項目



The following code creates an Array object letters with elements a, b, and c. The last element (c) is then removed from the array using the pop() method and assigned to the String object letter.

var letters:Array = new Array("a", "b", "c"); trace(letters); // a,b,c var letter:String = letters.pop(); trace(letters); // a,b trace(letter);     // c

push()メソッド 
AS3 function push(... args):uint

エレメントを配列の最後に追加して、追加後の配列の長さを返します。

パラメータ
... args — 配列に追加される値です。

戻り値
uint — 新しい配列の長さを表す整数です。

関連項目



The following code creates an empty Array object letters and then populates the array with the elements a, b, and c using the push() method.

var letters:Array = new Array();

letters.push("a"); letters.push("b"); letters.push("c");

trace(letters.toString()); // a,b,c

The following code creates an Array object letters, which is initially populated with the element a. The push() method is then used once to add the elements b and c to the end of the array, which is three elements after the push.

var letters:Array = new Array("a"); var count:uint = letters.push("b", "c");

trace(letters); // a,b,c trace(count);   // 3

reverse()メソッド 
AS3 function reverse():Array

配列の並びを反転させます。

戻り値
Array — 新しい配列です。


The following code creates an Array object letters with elements a, b, and c. The order of the array elements is then reversed using the reverse() method to produce the array [c,b,a].

var letters:Array = new Array("a", "b", "c"); trace(letters); // a,b,c letters.reverse(); trace(letters); // c,b,a

shift()メソッド 
AS3 function shift():Object

配列の最初のエレメントを削除して、そのエレメントを返します。残りの配列エレメントは、元の位置 i から i-1 に移動されます。

戻り値
Object — 配列内の最初のエレメント (任意のデータ型) です。

関連項目



The following code creates the Array object letters with elements a, b, and c. The shift() method is then used to remove the first element (a) from letters and assign it to the string firstLetter.

var letters:Array = new Array("a", "b", "c"); var firstLetter:String = letters.shift(); trace(letters);     // b,c trace(firstLetter); // a

slice()メソッド 
AS3 function slice(startIndex:int = 0, endIndex:int = 16777215):Array

元の配列から一連のエレメントを取り出して、新しい配列を返します。元の配列は変更されません。返される配列には、startIndex エレメントから endIndex エレメントまで (endIndex エレメント自体は除く) のすべてのエレメントが含まれます。

パラメータを何も渡さないと、元の配列の複製が作成されます。

パラメータ
startIndex:int (default = 0) — スライスの始点のインデックスを示す数値です。startIndex が負の数値の場合、始点は配列の末尾から開始します。つまり、-1 が最後のエレメントです。
 
endIndex:int (default = 16777215) — スライスの終点のインデックスを示す数値です。このパラメータを省略すると、スライスには配列の最初から最後までのすべてのエレメントが取り込まれます。endIndex が負の数値の場合、終点は配列の末尾から開始します。つまり、-1 が最後のエレメントです。

戻り値
Array — 元の配列から取り出した一連のエレメントから成る配列です。


The following code creates an Array object letters with elements [a,b,c,d,e,f]. The array someLetters is then created by calling the slice() method on elements one (b) through three (d), resulting in an array with elements b and c.

var letters:Array = new Array("a", "b", "c", "d", "e", "f"); var someLetters:Array = letters.slice(1,3);

trace(letters);     // a,b,c,d,e,f trace(someLetters); // b,c

The following code creates an Array object letters with elements [a,b,c,d,e,f].The array someLetters is then created by calling the slice() method on element two (c), resulting in an array with elements [c,d,e,f].

var letters:Array = new Array("a", "b", "c", "d", "e", "f"); var someLetters:Array = letters.slice(2);

trace(letters);     // a,b,c,d,e,f trace(someLetters); // c,d,e,f

The following code creates an Array object letters with elements [a,b,c,d,e,f]. The array someLetters is then created by calling the slice() method on the second to last element from the end (e), resulting in an array with elements e and f.

var letters:Array = new Array("a", "b", "c", "d", "e", "f"); var someLetters:Array = letters.slice(-2);

trace(letters);     // a,b,c,d,e,f trace(someLetters); // e,f

some()メソッド 
AS3 function some(callback:Function, thisObject:* = null):Boolean

true を返すアイテムに達するまで、配列内の各アイテムにテスト関数を実行します。このメソッドを使用して、配列内のいずれのアイテムもある基準 (値が特定の数値より小さいなど) を満たしているかどうかを判別できます。

このメソッドで、2 番目のパラメータ thisObject は、最初のパラメータ callback がメソッド閉包である場合、null でなければなりません。たとえば、me というムービークリップ内に、ある関数を作成するとします。

     function myFunction(){
        //your code here
     }
     

次に、myArray という配列に対して filter() メソッドを使用するとします。

     myArray.filter(myFunction, me);
     

myFunction は Timeline クラスのメンバーであり、me によってオーバーライドできないため、Flash Player は例外をスローします。このランタイムエラーは、次のように関数を変数に割り当てることによって回避できます。

     var foo:Function = myFunction() {
         //your code here
         };
     myArray.filter(foo, me);
     
パラメータ
callback:Function — 配列内の各アイテムについて実行する関数です。この関数には、単純な比較 (item < 20 など) やより複雑な演算を含めることができます。また、この関数は、次のように、アイテムの値、アイテムのインデックス、および Array オブジェクトの 3 つの引数を使用して呼び出されます。
    function callback(item:*, index:int, array:Array):Boolean;
 
thisObject:* (default = null) — 関数の this として使用するオブジェクトです。

戻り値
Boolean — ブール値です。指定された関数について配列内のいずれかのアイテムが true を返す場合は true、それ以外の場合は false を返します。

関連項目



The following example displays which values are undefined:
 package { import flash.display.Sprite; public class Array_some extends Sprite { public function Array_some() { var arr:Array = new Array(); arr[0] = "one"; arr[1] = "two"; arr[3] = "four"; var isUndef:Boolean = arr.some(isUndefined); if (isUndef) { trace("array contains undefined values: " + arr); } else { trace("array contains no undefined values."); } } private function isUndefined(element:*, index:int, arr:Array):Boolean { return (element == undefined); } } }

sort()メソッド 
AS3 function sort(... args):Array

配列内のエレメントをソートします。このメソッドは、Unicode 値に基づいてソートを実行します。(ASCII は Unicode のサブセットです。)

デフォルトでは、Array.sort() は次のように動作します。

デフォルト設定とは別の設定を使用して配列をソートする場合は、...args パラメータ記述の sortOptions 部分に記述されているソートオプションのいずれかを使用するか、またはソート処理を行う独自のカスタム関数を作成できます。カスタム関数を作成する場合、sort() メソッドを呼び出して、カスタム関数の名前を最初の引数 (compareFunction) として使用します。

パラメータ
... args — 比較関数を指定する引数と、ソート動作を決定する値です。

このメソッドでは、Array.sort(compareFunction, sortOptions) というシンタックスおよび引数順を使用し、各引数は次のように定義されます。

  • compareFunction - 配列内のエレメントのソート順を決定する比較関数。このパラメータは省略可能です。比較関数には、比較を行うために 2 つの引数が必要です。エレメント A と B がある場合、compareFunction は次の 3 つの値のいずれかを返します。
    • A が B の前に表示されるソート順の場合は -1
    • A = B の場合は 0
    • A が B の後に表示されるソート順の場合は 1
  • sortOptions - デフォルトのソート動作を変更する数値または定義済み定数。複数指定する場合は、ビット単位の論理和 (OR)| 演算子で区切ります。このパラメータは省略可能です。sortOptions の許容値は次のとおりです。
    • 1 または Array.CASEINSENSITIVE
    • 2 または Array.DESCENDING
    • 4 または Array.UNIQUESORT
    • 8 または Array.RETURNINDEXEDARRAY
    • 16 または Array.NUMERIC
    詳細については、Array.sortOn() メソッドを参照してください。

メモ : Array.sort() メソッドは ECMAScript (ECMA-262) Edition 3 言語仕様で定義されていますが、Flash Player 7 で導入された配列ソートオプションは Flash 固有であり、ECMA-262 を拡張したものです。

戻り値
Array — 戻り値は、次に示すように、渡された引数によって異なります。
  • ...args パラメータの sortOptions 引数に値 4 または Array.UNIQUESORT を指定すると、ソート対象のエレメントに同じソートフィールドを持つものが複数ある場合、値 0 が返されます。配列は変更されません。
  • ...args パラメータの sortOptions 引数に値 8 または Array.RETURNINDEXEDARRAY を指定した場合、ソート結果を反映したインデックスの数値配列が返されます。配列は変更されません。
  • それ以外の場合、値は返されません。ソート順を反映するように配列が変更されます。

関連項目



The following code creates the Array object vegetables with elements [spinach, green pepper, cilantro, onion, avocado]. The array is then sorted by the sort() method, which is called with no parameters. The result is vegetables sorted in alphabetical order ([avocado, cilantro, green pepper, onion, spinach]).

var vegetables:Array = new Array("spinach", "green pepper", "cilantro", "onion", "avocado");

trace(vegetables); // spinach,green pepper,cilantro,onion,avocado vegetables.sort(); trace(vegetables); // avocado,cilantro,green pepper,onion,spinach

The following code creates the Array object vegetables with elements [spinach, green pepper, Cilantro, Onion, and Avocado]. The array is then sorted by the sort() method, which is called with no parameters the first time; the result is [Avocado,Cilantro,Onion,green pepper,spinach]. Then sort() is called on vegetables again with the CASEINSENSITIVE constant as a parameter. The result is vegetables sorted in alphabetical order ([Avocado, Cilantro, green pepper, Onion, spinach]).

var vegetables:Array = new Array("spinach", "green pepper", "Cilantro", "Onion", "Avocado");

vegetables.sort(); trace(vegetables); // Avocado,Cilantro,Onion,green pepper,spinach vegetables.sort(Array.CASEINSENSITIVE); trace(vegetables); // Avocado,Cilantro,green pepper,Onion,spinach

The following code creates the empty Array object vegetables, which is then populated through five calls to push(). Each time push() is called, a new Vegetable object is created by a call to the Vegetable() constructor, which accepts a String (name) and Number (price) object. Calling push() five times with the values shown results in the following array: [lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44]. The sort() method is then used to sort the array, resulting in the array [asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44].
 var vegetables:Array = new Array(); vegetables.push(new Vegetable("lettuce", 1.49)); vegetables.push(new Vegetable("spinach", 1.89)); vegetables.push(new Vegetable("asparagus", 3.99)); vegetables.push(new Vegetable("celery", 1.29)); vegetables.push(new Vegetable("squash", 1.44));

trace(vegetables); // lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

vegetables.sort();

trace(vegetables); // asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44

//The following code defines the Vegetable class class Vegetable { private var name:String; private var price:Number;

    public function Vegetable(name:String, price:Number) { this.name = name; this.price = price; }

    public function toString():String { return " " + name + ":" + price; } }

The following example is exactly the same as the previous one, except that the sort() method is used with a custom sort function (sortOnPrice), which sorts according to price instead of alphabetically. Note that the new function getPrice() extracts the price.

var vegetables:Array = new Array(); vegetables.push(new Vegetable("lettuce", 1.49)); vegetables.push(new Vegetable("spinach", 1.89)); vegetables.push(new Vegetable("asparagus", 3.99)); vegetables.push(new Vegetable("celery", 1.29)); vegetables.push(new Vegetable("squash", 1.44));

trace(vegetables); // lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

vegetables.sort(sortOnPrice);

trace(vegetables); // celery:1.29, squash:1.44, lettuce:1.49, spinach:1.89, asparagus:3.99

function sortOnPrice(a:Vegetable, b:Vegetable):Number { var aPrice:Number = a.getPrice(); var bPrice:Number = b.getPrice();

    if(aPrice > bPrice) { return 1; } else if(aPrice < bPrice) { return -1; } else  { //aPrice == bPrice return 0; } }

// The following code defines the Vegetable class and should be in a separate package. class Vegetable { private var name:String; private var price:Number;

    public function Vegetable(name:String, price:Number) { this.name = name; this.price = price; }

    public function getPrice():Number { return price; }

    public function toString():String { return " " + name + ":" + price; } }

The following code creates the Array object numbers with elements [3,5,100,34,10]. A call to sort() without any parameters sorts alphabetically, producing the undesired result [10,100,3,34,5]. To properly sort numeric values, you must pass the constant NUMERIC to the sort() method, which sorts numbers as follows: [3,5,10,34,100].

Note: The default behavior of the sort() function is to handle each entity as a string. The Array.NUMERIC argument does not actually convert other data types to the Number data type; it simply allows the sort algorithm to recognize numbers.


var numbers:Array = new Array(3,5,100,34,10);

trace(numbers); // 3,5,100,34,10 numbers.sort(); trace(numbers); // 10,100,3,34,5 numbers.sort(Array.NUMERIC); trace(numbers); // 3,5,10,34,100

sortOn()メソッド 
AS3 function sortOn(fieldName:Object, options:Object = null):Array

配列内のフィールド (複数のフィールドも可能) に基づいて、配列内のエレメントをソートします。配列は、次に示す特性を備えている必要があります。

fieldName パラメータを複数指定する場合、先頭のフィールドが第 1 ソートフィールド、2 番目のフィールドが第 2 ソートフィールド、(以下同様) と見なされます。ソートは Unicode 値に基づいて実行されます。(ASCII は Unicode のサブセットです。)fieldName パラメータで指定されたフィールドが、比較対象のいずれのエレメントにも含まれていない場合、そのフィールドは undefined に設定されていると見なされます。ソート済みの配列では、エレメントが連続的かつランダムに格納されます。

デフォルトでは、Array.sortOn() は次のように動作します。

Flash Player 7 では options パラメータが追加されました。このパラメータを使用すると、デフォルトのソート動作をオーバーライドすることができます。単純な配列 (たとえば、1 つのフィールドだけを持つ配列) をソートする場合、または options パラメータでサポートされていないソート順序を指定する場合、Array.sort() を使用します。

複数のフラグを渡すには、ビット単位の論理和 (OR)(|) 演算子で区切ります。

  my_array.sortOn(someFieldName, Array.DESCENDING | Array.NUMERIC);
  

Flash Player 8 では、複数のフィールドでソートを行う場合に、各フィールドに対して異なるソートオプションを指定する機能が追加されました。Flash Player 8 以降では、options パラメータはさまざまなソートオプションの配列を受け入れます。各ソートオプションは fieldName パラメータのソートフィールドに対応します。次の例では、第 1 ソートフィールド a を降順で、第 2 ソートフィールド b を数値ソートで、第 3 ソートフィールド c を大文字と小文字を区別しないでソートします。

  Array.sortOn (["a", "b", "c"], [Array.DESCENDING, Array.NUMERIC, Array.CASEINSENSITIVE]);
  

メモ : fieldName 配列と options 配列のエレメントは同数である必要があります。そうでない場合、options 配列は無視されます。また、Array.UNIQUESORT オプションと Array.RETURNINDEXEDARRAY オプションは、配列内の 1 番目のエレメントとしてのみ使用できます。そうでない場合、これらのオプションは無視されます。

パラメータ
fieldName:Object — ソート値として使用するフィールドを示すストリング、または、先頭のエレメントが第 1 ソートフィールド、2 番目が第 2 ソートフィールド (以下同様) を表す配列です。
 
options:Object (default = null)bitwise OR (|) 演算子によって区切られ、ソート動作を変更する定義済み定数の数値または名前です。options パラメータには次の値を指定できます。

数値形式 (2) ではなく、ストリング形式のフラグ (DESCENDING など) を使用すると、コードヒントが有効になります。

戻り値
Array — 戻り値は、パラメータを渡したかどうかによって異なります。
  • options パラメータの値として 4 (Array.UNIQUESORT) を指定し、ソート対象の複数のエレメントにまったく同じソートフィールドがある場合は値 0 が返されます。配列は変更されません。
  • options パラメータの値として 8 (Array.RETURNINDEXEDARRAY) を指定した場合、そのソート結果を反映する配列が返されます。配列は変更されません。
  • これ以外の場合、何も返されず、ソート順を反映するよう配列が変更されます。

関連項目



The following code creates an empty Array object vegetables and the array is then populated using five calls to push(). Each time push() is called, a new Vegetable object is created by calling the Vegetable() constructor, which accepts a String (name) and Number (price) object. Calling push() five times with the values shown results in the following array: [lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44]. The sortOn() method is then used with the name parameter to produce the following array: [asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44]. The sortOn() method is then called again with the price parameter, and the NUMERIC and DESCENDING constants to produce an array sorted by numbers in descending order: [asparagus:3.99, spinach:1.89, lettuce:1.49, squash:1.44, celery:1.29].

var vegetables:Array = new Array(); vegetables.push(new Vegetable("lettuce", 1.49)); vegetables.push(new Vegetable("spinach", 1.89)); vegetables.push(new Vegetable("asparagus", 3.99)); vegetables.push(new Vegetable("celery", 1.29)); vegetables.push(new Vegetable("squash", 1.44));

trace(vegetables); // lettuce:1.49, spinach:1.89, asparagus:3.99, celery:1.29, squash:1.44

vegetables.sortOn("name"); trace(vegetables); // asparagus:3.99, celery:1.29, lettuce:1.49, spinach:1.89, squash:1.44

vegetables.sortOn("price", Array.NUMERIC | Array.DESCENDING); trace(vegetables); // asparagus:3.99, spinach:1.89, lettuce:1.49, squash:1.44, celery:1.29

class Vegetable { public var name:String; public var price:Number;

    public function Vegetable(name:String, price:Number) { this.name = name; this.price = price; }

    public function toString():String { return " " + name + ":" + price; } }

The following code creates an empty Array object records and the array is then populated using three calls to push(). Each time push() is called, the strings name and city and a zip number are added to records. Three for loops are used to print the array elements. The first for loop prints the elements in the order in which they were added. The second for loop is run after records has been sorted by name and then city using the sortOn() method. The third for loop produces different output because records is re-sorted by city then by name.


var records:Array = new Array(); records.push({name:"john", city:"omaha", zip:68144}); records.push({name:"john", city:"kansas city", zip:72345}); records.push({name:"bob", city:"omaha", zip:94010});

for(var i:uint = 0; i < records.length; i++) { trace(records[i].name + ", " + records[i].city); } // Results: // john, omaha // john, kansas city // bob, omaha

trace("records.sortOn('name', 'city');"); records.sortOn(["name", "city"]); for(var i:uint = 0; i < records.length; i++) { trace(records[i].name + ", " + records[i].city); } // Results: // bob, omaha // john, kansas city // john, omaha

trace("records.sortOn('city', 'name');"); records.sortOn(["city", "name"]); for(var i:uint = 0; i < records.length; i++) { trace(records[i].name + ", " + records[i].city); } // Results: // john, kansas city // bob, omaha // john, omaha

The following code creates an empty Array object users and the array is then populated using four calls to push(). Each time push() is called, a User object is created with the User() constructor and a name string and age uint are added to users. The resulting array set is [Bob:3,barb:35,abcd:3,catchy:4].

The array is then sorted in the following ways:

  1. By name only, producing the array [Bob:3,abcd:3,barb:35,catchy:4]
  2. By name and using the CASEINSENSITIVE constant, producing the array [abcd:3,barb:35,Bob:3,catchy:4]
  3. By name and using the CASEINSENSITIVE and DESCENDING constants, producing the array [catchy:4,Bob:3,barb:35,abcd:3]
  4. By age only, producing the array [abcd:3,Bob:3,barb:35,catchy:4]
  5. By age and using the NUMERIC constant, producing the array [Bob:3,abcd:3,catchy:4,barb:35]
  6. By age and using the DESCENDING and NUMERIC constants, producing the array [barb:35,catchy:4,Bob:3,abcd:3]

An array called indices is then created and assigned the results of a sort by age and using the NUMERIC and RETURNINDEXEDARRAY constants, resulting in the array [Bob:3,abcd:3,catchy:4,barb:35], which is then printed out using a for loop.


class User { public var name:String; public var age:Number; public function User(name:String, age:uint) { this.name = name; this.age = age; }

    public function toString():String { return this.name + ":" + this.age; } }

var users:Array = new Array(); users.push(new User("Bob", 3)); users.push(new User("barb", 35)); users.push(new User("abcd", 3)); users.push(new User("catchy", 4));

trace(users); // Bob:3,barb:35,abcd:3,catchy:4

users.sortOn("name"); trace(users); // Bob:3,abcd:3,barb:35,catchy:4

users.sortOn("name", Array.CASEINSENSITIVE); trace(users); // abcd:3,barb:35,Bob:3,catchy:4

users.sortOn("name", Array.CASEINSENSITIVE | Array.DESCENDING); trace(users); // catchy:4,Bob:3,barb:35,abcd:3

users.sortOn("age"); trace(users); // abcd:3,Bob:3,barb:35,catchy:4

users.sortOn("age", Array.NUMERIC); trace(users); // Bob:3,abcd:3,catchy:4,barb:35

users.sortOn("age", Array.DESCENDING | Array.NUMERIC); trace(users); // barb:35,catchy:4,Bob:3,abcd:3

var indices:Array = users.sortOn("age", Array.NUMERIC | Array.RETURNINDEXEDARRAY); var index:uint; for(var i:uint = 0; i < indices.length; i++) { index = indices[i]; trace(users[index].name, ": " + users[index].age); }

// Results: // Bob : 3 // abcd : 3 // catchy : 4 // barb : 35

splice()メソッド 
AS3 function splice(startIndex:int, deleteCount:uint, ... values):Array

配列のエレメントを追加および削除します。このメソッドは、コピーを作成しないで、配列を変更します。

メモ : Array のサブクラス内でこのメソッドをオーバーライドするには、次の例に示すように、パラメータに ...args を使用します。

  public override function splice(...args) {
    // your statements here
  }
  
パラメータ
startIndex:int — 挿入または削除を開始する配列エレメントのインデックスを示す整数です。負の整数を使用すると、配列の末尾を基準として位置を指定できます。たとえば、-1 は配列の最後のエレメントです。
 
deleteCount:uint — 削除するエレメント数を示す整数です。この数には、startIndex パラメータで指定するエレメントが含まれます。deleteCount パラメータに値を指定しないと、startIndex パラメータで指定した配列エレメントから最後の配列エレメントまでの値がすべて削除されます。値として 0 を指定すると、エレメントは削除されません。
 
... valuesstartIndex パラメータで指定した配列内の位置に挿入するカンマ区切り値または配列のオプションリストです。

戻り値
Array — 元の配列から削除されたエレメントを含む配列です。


The following code creates the Array object vegetables with the elements [spinach, green pepper, cilantro, onion, avocado]. The splice() method is then called with the parameters 2 and 2, which assigns cilantro and onion to the spliced array. The vegetables array then contains [spinach,green pepper,avocado]. The splice() method is called a second time using the parameters 1, 0, and the spliced array to assign [cilantro,onion] as the second element in vegetables.

var vegetables:Array = new Array("spinach", "green pepper", "cilantro", "onion", "avocado");

var spliced:Array = vegetables.splice(2, 2); trace(vegetables); // spinach,green pepper,avocado trace(spliced);    // cilantro,onion

vegetables.splice(1, 0, spliced); trace(vegetables); // spinach,cilantro,onion,green pepper,avocado

Notice that cilantro and onion trace out as if vegetables has 5 elements, even though it actually has four (and the second element is another array containing two elements). To add cilantro and onion individually, you would use:
 
var vegetables:Array = new Array("spinach", "green pepper", "cilantro", "onion", "avocado");
 
 var spliced:Array = vegetables.splice(2, 2); trace(vegetables); // spinach,green pepper,avocado trace(spliced);    // cilantro,onion
 
 vegetables.splice(1, 0, "cilantro", "onion"); trace(vegetables); // spinach,cilantro,onion,green pepper,avocado

toLocaleString()メソッド 
public function toLocaleString():String

指定された配列内のエレメントを表すストリングを返します。インデックス 0 から最大インデックスまでの配列内のすべてのエレメントを、カンマで区切られた連結ストリングに変換して返します。ActionScript 3.0 実装において、このメソッドは、Array.toString() と同じ値を返します。

戻り値
String — 配列エレメントのストリングです。

関連項目

toString()メソッド 
public function toString():String

指定された配列内のエレメントを表すストリングを返します。インデックス 0 から最大インデックスまでの配列内のすべてのエレメントを、カンマで区切られた連結ストリングに変換して返します。カスタムセパレータを指定するには、Array.join() メソッドを使用します。

戻り値
String — 配列エレメントのストリングです。

関連項目



The following code creates an Array, converts the values to strings, and stores them in the vegnums variable of the String data type.

var vegetables:Array = new Array(); vegetables.push(1); vegetables.push(2); vegetables.push(3); vegetables.push(4); vegetables.push(5); var vegnums:String = vegetables.toString(); trace(vegnums+",6"); // 1,2,3,4,5,6

unshift()メソッド 
AS3 function unshift(... args):uint

エレメントを配列の先頭に追加して、配列の新しい長さを返します。配列内の他のエレメントは、元の位置 i から i-1 に移動されます。

パラメータ
... args — 配列の先頭に挿入される数値、エレメント、または変数です。

戻り値
uint — 配列の新しい長さを表す整数です。

関連項目



The following code creates the empty Array object names. The strings Bill and Jeff are added by the push() method, and then the strings Alfred and Kyle are added to the beginning of names by two calls to the unshift() method.

var names:Array = new Array(); names.push("Bill"); names.push("Jeff");

trace(names); // Bill,Jeff

names.unshift("Alfred"); names.unshift("Kyle");

trace(names); // Kyle,Alfred,Bill,Jeff

定数の詳細
CASEINSENSITIVE定数
public static const CASEINSENSITIVE:uint = 1

Array クラスのソートメソッドに対して、大文字と小文字を区別しないソートを指定します。この定数は、sort() メソッドまたは sortOn() メソッドの options パラメータに使用できます。

この定数の値は 1 です。

関連項目

DESCENDING定数 
public static const DESCENDING:uint = 2

Array クラスのソートメソッドに対して、降順でのソートを指定します。この定数は、sort() メソッドまたは sortOn() メソッドの options パラメータに使用できます。

この定数の値は 2 です。

関連項目

NUMERIC定数 
public static const NUMERIC:uint = 16

Array クラスのソートメソッドに対して、文字ストリングではなく数値によるソートを指定します。この定数を options パラメータに設定すると、sort() メソッドと sortOn() メソッドは、数字を文字ストリングとしてではなく、数値としてソートします。NUMERIC 定数を設定しないでソートを実行すると、各配列エレメントは文字ストリングとして処理され、Unicode 順でソートされます。

たとえば、値 [2005, 7, 35] の配列で、options パラメータに NUMERIC 定数を設定しない場合、ソート後の配列は [2005, 35, 7] となります。一方、NUMERIC 定数を設定した場合、ソート後の配列は [7, 35, 2005] となります。

この定数は、配列内の数値に対してのみ適用されます。["23", "5"] などの、数値データを含むストリングには適用されません。

この定数の値は 16 です。

関連項目

RETURNINDEXEDARRAY定数 
public static const RETURNINDEXEDARRAY:uint = 8

ソート結果として、配列インデックスで構成される配列を返すことを指定します。sort() メソッドまたは sortOn() メソッドの options パラメータにこの定数を使用すると、元の配列を変更せずに、配列エレメントの複数のビューにアクセスすることができます。

この定数の値は 8 です。

関連項目

UNIQUESORT定数 
public static const UNIQUESORT:uint = 4

Array クラスのソートメソッドに対して、一意性ソート要件を指定します。この定数は、sort() メソッドまたは sortOn() メソッドの options パラメータに使用できます。一意性ソートオプションを指定すると、ソート対象の任意の 2 つのエレメントまたはフィールドが同じ値である場合に、ソートが終了します。

この定数の値は 4 です。

関連項目


The following example creates a new Array object myArr with no arguments and an initial length of 0:
 package { import flash.display.Sprite;

    public class ArrayExample extends Sprite { public function ArrayExample() { var myArr:Array = new Array(); trace(myArr.length); // 0 } } }