User Guide

246 Working with Arrays
{
this[i] = args[i]
}
}
}
}
The TypedArray constructor shares most of the code from the Array constructor, with only
four changes to the code. First, the parameter list includes a new required parameter of type
Class that allows specification of the array’s data type. Second, the data type passed to the
constructor is assigned to the
dataType variable. Third, in the else statement, the value of
the
length property is assigned after the for loop so that length includes only arguments
that are the proper type. Fourth, the body of the
for loop uses the overridden version of the
push() method so that only arguments of the correct data type are added to the array. The
following example shows the TypedArray constructor function:
public dynamic class TypedArray extends Array
{
private var dataType:Class;
public function TypedArray(typeParam:Class, ...args)
{
dataType = typeParam;
var n:uint = args.length
if (n == 1 && (args[0] is Number))
{
var dlen:Number = args[0];
var ulen:uint = dlen
if (ulen != dlen)
{
throw new RangeError("Array index is not a 32-bit unsigned integer
("+dlen+")")
}
length = ulen;
}
else
{
for (var i:int=0; i < n; i++)
{
// type check done in push()
this.push(args[i])
}
length = this.length;
}
}
}