package
{
import __AS3__.vec.Vector;
import flash.display.Sprite;
import flash.text.TextField;
[SWF(width=800, height=600, backgroundColor=0xffffff)]
public class AstroVector extends Sprite
{
public function AstroVector()
{
_info = new TextField();
_info.width = 800;
_info.height = 600;
addChild(_info);
// Primitive Vector
{
var pv:Vector.<int> = new Vector.<int>();
pv.push(1);
pv.push(2);
pv.push(true); // is correct.
pv.push(false); // Boolean will cast to int.
pv.push('hoge'); // String too.
trace(String(pv)); // 1,2,1,0,0
}
// Custom Class Vector
{
var a:* = new A();
var b:* = new B();
var c:* = new C();
var v:Vector.<A> = new Vector.<A>();
v.push(a);
v.push(b);
try {
v.push(c); // is error. C can't cast to A.
// but null is pushed instead.
}
catch (e:*) {
trace(e.toString()); // TypeError
}
trace(String(v.length)); // 3. It isn't 2.
trace(String(v)); // A, B, null
}
// Cast
{
// Array to Vector
var atv:Vector.<A> = Vector.<A>([new A(), new B()]);
trace(String(atv.length)) // 2
trace(String(atv)); // A, B
// Vector to Array
// is impossible...?
}
// Length
{
var vl:Vector.<int> = new Vector.<int>(2);
vl[0] = 1; // OK
vl[1] = 2; // OK
try {
vl[3] = 4; // Bad. Out of range.
// Index must be (0 <= index <= length + 1).
}
catch (e2:*) {
trace(e2.toString()); // RangeError
}
vl[2] = 3; // OK
vl[3] = 4; // OK. Because now length+1 is 3.
}
// Fixed
{
var vf:Vector.<int> = new Vector.<int>(2);
vf.fixed = true;
vf[0] = 1; // OK
vf[1] = 2; // OK
try {
vf[2] = 3; // Bad. Length is fixed.
}
catch (e3:*) {
trace(e3.toString()); // RangeError
}
try {
vf.push(3); // Bad. Length is fixed.
}
catch (e4:*) {
trace(e4.toString()); // RangeError
}
}
}
private var _info:TextField;
private function trace(s:String):void
{
_info.appendText(s + "\n");
}
}
}
class A
{
public function toString():String
{
return 'A';
}
}
class B extends A
{
public override function toString():String
{
return 'B';
}
}
class C
{
public function toString():String
{
return 'C';
}
}