静岡理工科大学 菅沼ホーム ActionScript 目次 索引

Array クラス

[内容]

  配列の処理を行うためのクラスです.

継承: Array → Object

[定数]  すべて,static

[プロパティ]

[メソッド]

[使用例]

  1. 使用例1: concat,indexOf, lastIndexOf,pop,push,shift,unshift,slice,splice,join,toString -> 表示とそのソースコード
    ackage {
    	import flash.display.Sprite;
    
    	[SWF(backgroundColor="0xeeffee", width="500", height="200", frameRate="30")]
    
    	public class Array_e1 extends Sprite {
    
    		public function Array_e1() {
    			var msg : Message = new Message(500, 100);
    			addChild(msg);
    			var a : Array = new Array(1.5, 2.3, 3.4, 2.3);
    			var i1 : int;
    			var p1 : int;
    			var p2 : int;
    			var str : String;
    					// concat
    			var b : Array = a.concat(-1.0, -2.5);
    			msg.tx.appendText("concat(b) : ");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText(b[i1] + " ");
    			msg.tx.appendText("\n");
    					// indexOf, lastIndexOf
    			p1 = b.indexOf(2.3);
    			p2 = b.lastIndexOf(2.3);
    			msg.tx.appendText("indexOf: " + p1 + ", lawstIndexOf: " + p2 + "\n");
    					// pop
    			b.pop();
    			b.pop();
    			msg.tx.appendText("pop(b) : ");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText(b[i1] + " ");
    			msg.tx.appendText("\n");
    					// push
    			b.push(-1.0, -2.5);
    			msg.tx.appendText("push(b) : ");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText(b[i1] + " ");
    			msg.tx.appendText("\n");
    					// shift
    			b.shift();
    			b.shift();
    			msg.tx.appendText("shift(b) : ");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText(b[i1] + " ");
    			msg.tx.appendText("\n");
    					// unshift
    			b.unshift(1.5, 2.3);
    			msg.tx.appendText("unshift(b) : ");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText(b[i1] + " ");
    			msg.tx.appendText("\n");
    					// slice
    			var c : Array = b.slice(3, 5);
    			msg.tx.appendText("slice : ");
    			for (i1 = 0; i1 < c.length; i1++)
    				msg.tx.appendText(c[i1] + " ");
    			msg.tx.appendText("\n");
    					// splice
    			var d : Array = b.splice(0, 3, 20, 10);
    			msg.tx.appendText("splice : ");
    			for (i1 = 0; i1 < d.length; i1++)
    				msg.tx.appendText(d[i1] + " ");
    			msg.tx.appendText("\n");
    					// join
    			str = b.join(";");
    			msg.tx.appendText("join : " + str + "\n");
    					// toString
    			str = b.toString();
    			msg.tx.appendText("toString : " + str + "\n");
    		}
    	}
    }
    
    package
    {
    	import flash.display.Sprite;
    	import flash.text.TextField;
    	import flash.text.TextFieldType;
    	import flash.text.TextFormat;
    	import flash.text.TextFormatAlign;
    	import flash.events.MouseEvent;
    
    	public class Message extends Sprite
    	{
    		public var tx : TextField;
    		public function Message(w : int, h : int)
    		{
    			tx = new TextField();
    			tx.width = w;
    			tx.height = h;
    			tx.x = 0;
    			tx.y = 0;
    			tx.type = TextFieldType.DYNAMIC;
    			tx.background = true;
    			tx.backgroundColor = 0xffffff;
    			tx.border = true;
    			tx.borderColor = 0x00ff00;
    			tx.multiline = true;
    			var tf : TextFormat = new TextFormat();
    			tf.size = 20;
    			tx.defaultTextFormat = tf;
    			addChild(tx);
    			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    		}
    
    		private function onMouseDown(event:MouseEvent):void
    		{
    			startDrag();
    			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    
    		private function onMouseMove(event:MouseEvent):void
    		{
    			event.updateAfterEvent();
    		}
    
    		private function onMouseUp(event:MouseEvent):void
    		{
    			stopDrag();
    			removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    	}
    }
    			

  2. 使用例2: forEach,map,every,some,filter -> 表示とそのソースコード
    package {
    	import flash.display.Sprite;
    
    	[SWF(backgroundColor="0xeeffee", width="500", height="200", frameRate="30")]
    
    	public class Array_e2 extends Sprite {
    
    		public function Array_e2() {
    			var msg : Message = new Message(500, 100);
    			addChild(msg);
    			var a : Array = new Array(1, 2, 3, -1, -2, 20, 10);
    			var b : Array = new Array(1, 2, 3, 2, 1);
    			var i1 : int;
    					// forEach
    			msg.tx.appendText("forEach(a) : ");
    			a.forEach(check2);
    			msg.tx.appendText("\n");
    			msg.tx.appendText("forEach(b) : ");
    			b.forEach(check2);
    			msg.tx.appendText("\n");
    					// map
    			msg.tx.appendText("map(a) : ");
    			var d : Array = a.map(check3);
    			for (i1 = 0; i1 < d.length; i1++)
    				msg.tx.appendText(d[i1] + " ");
    			msg.tx.appendText("\n");
    					// every
    			var ra : String = a.every(check1).toString();
    			var rb : String = b.every(check1).toString();
    			msg.tx.appendText("every : a:" + ra + ", b:" + rb + "\n");
    					// some
    			ra = a.some(check1).toString();
    			rb = b.some(check1).toString();
    			msg.tx.appendText("some : a:" + ra + ", b:" + rb + "\n");
    					// filter
    			var c : Array = a.filter(check1);
    			msg.tx.appendText("filter : ");
    			for (i1 = 0; i1 < c.length; i1++)
    				msg.tx.appendText(c[i1] + " ");
    			msg.tx.appendText("\n");
    
    			function check1(item:*, index:int, array:Array):Boolean
    			{
    				return item > 0;
    			}
    
    			function check2(item:*, index:int, array:Array):void
    			{
    				msg.tx.appendText(item + " ");
    			}
    
    			function check3(item:*, index:int, array:Array):int
    			{
    				return item;
    			}
    		}
    	}
    }
    
    package
    {
    	import flash.display.Sprite;
    	import flash.text.TextField;
    	import flash.text.TextFieldType;
    	import flash.text.TextFormat;
    	import flash.text.TextFormatAlign;
    	import flash.events.MouseEvent;
    
    	public class Message extends Sprite
    	{
    		public var tx : TextField;
    		public function Message(w : int, h : int)
    		{
    			tx = new TextField();
    			tx.width = w;
    			tx.height = h;
    			tx.x = 0;
    			tx.y = 0;
    			tx.type = TextFieldType.DYNAMIC;
    			tx.background = true;
    			tx.backgroundColor = 0xffffff;
    			tx.border = true;
    			tx.borderColor = 0x00ff00;
    			tx.multiline = true;
    			var tf : TextFormat = new TextFormat();
    			tf.size = 20;
    			tx.defaultTextFormat = tf;
    			addChild(tx);
    			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    		}
    
    		private function onMouseDown(event:MouseEvent):void
    		{
    			startDrag();
    			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    
    		private function onMouseMove(event:MouseEvent):void
    		{
    			event.updateAfterEvent();
    		}
    
    		private function onMouseUp(event:MouseEvent):void
    		{
    			stopDrag();
    			removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    	}
    }
    			

  3. 使用例3: reverse,sort,sortOn -> 表示とそのソースコード
    package {
    	import flash.display.Sprite;
    
    	[SWF(backgroundColor="0xeeffee", width="500", height="200", frameRate="30")]
    
    	public class Array_e3 extends Sprite {
    
    		public function Array_e3() {
    			var msg : Message = new Message(500, 100);
    			addChild(msg);
    			var a : Array = new Array(1, 5, 3, 0);
    			var i1 : int;
    					// reverse
    			a.reverse();
    			msg.tx.appendText("reverse : ");
    			for (i1 = 0; i1 < a.length; i1++)
    				msg.tx.appendText(a[i1] + " ");
    			msg.tx.appendText("\n");
    					// sort(昇順)
    			a.sort();
    			msg.tx.appendText("sort(昇順) : ");
    			for (i1 = 0; i1 < a.length; i1++)
    				msg.tx.appendText(a[i1] + " ");
    			msg.tx.appendText("\n");
    					// sort(降順,ソートオプション)
    			a.sort(Array.DESCENDING);
    			msg.tx.appendText("sort(降順,ソートオプション) : ");
    			for (i1 = 0; i1 < a.length; i1++)
    				msg.tx.appendText(a[i1] + " ");
    			msg.tx.appendText("\n");
    					// sort(降順,比較関数)
    			a.sort(comp);
    			msg.tx.appendText("sort(降順,比較関数) : ");
    			for (i1 = 0; i1 < a.length; i1++)
    				msg.tx.appendText(a[i1] + " ");
    			msg.tx.appendText("\n");
    					// sortOn
    			var b : Array = new Array(3);
    			b.push(new Complex(19.2, 1.2));
    			b.push(new Complex(1.7, 4.5));
    			b.push(new Complex(3.5, 10.5));
    			b.sortOn("real", Array.NUMERIC);
    			msg.tx.appendText("sortOn : \n");
    			for (i1 = 0; i1 < b.length; i1++)
    				msg.tx.appendText("   (" + b[i1].real + ", " + b[i1].img + ")\n");
    
    			function comp(a:int, b:int):int
    			{
    				return b - a;
    			}
    		}
    	}
    }
    
    package {
    	public class Complex {
    		public var real : Number;
    		public var img : Number;
    		public function Complex(x:Number, y:Number) {
    			this.real = x;
    			this.img  = y;
    		}
    	}
    }
    
    package
    {
    	import flash.display.Sprite;
    	import flash.text.TextField;
    	import flash.text.TextFieldType;
    	import flash.text.TextFormat;
    	import flash.text.TextFormatAlign;
    	import flash.events.MouseEvent;
    
    	public class Message extends Sprite
    	{
    		public var tx : TextField;
    		public function Message(w : int, h : int)
    		{
    			tx = new TextField();
    			tx.width = w;
    			tx.height = h;
    			tx.x = 0;
    			tx.y = 0;
    			tx.type = TextFieldType.DYNAMIC;
    			tx.background = true;
    			tx.backgroundColor = 0xffffff;
    			tx.border = true;
    			tx.borderColor = 0x00ff00;
    			tx.multiline = true;
    			var tf : TextFormat = new TextFormat();
    			tf.size = 20;
    			tx.defaultTextFormat = tf;
    			addChild(tx);
    			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
    		}
    
    		private function onMouseDown(event:MouseEvent):void
    		{
    			startDrag();
    			addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    
    		private function onMouseMove(event:MouseEvent):void
    		{
    			event.updateAfterEvent();
    		}
    
    		private function onMouseUp(event:MouseEvent):void
    		{
    			stopDrag();
    			removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
    			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
    		}
    	}
    }
    			

静岡理工科大学 菅沼ホーム ActionScript 目次 索引