Not yet rated

Problem

I would like to find a way I can create an initialized dynamic size array. It shouldn't be using loops of course. I thought maybe overriding the base array class with somehow to pass to parameters 1) a number for repeat and 2) item as the initializer. And when a user requests at item which is not defined the overriding class will return the item instead of undefined or exception. is this possible in a safe way?

Solution

By default value of all elements of the newly created instance of the Array class are "undefine" . In many cases checking for value “undefined” when access an element of array is a waste of time. In order to remove this check the array should have valid items. So there are two ways to do that: 

Detailed explanation


The first way is to iterate all elements in array and initialize it ( initializing can be setting a value or creating a instance of some class ). In this way all elements of the array will contain a valid data.
Second way is to extend Array class and check for the "undefine" when someone access the element. In this way the array may contain an undefined elements but if someone tries to access it they will be initialized. Unfortunately this way cannot be applied here  because in AS3 operators cannot be override ( we need to override operator [] ) and functions cannot be overloaded ( we need a constructor with two parameters - size and type. We can use second constructor of the Array class and handle the situation when instance is created with two parameters and the second parameter is a type ).
So the solution is to implement  the first way with  a function that will iterate all elements of the array and initialize it. The iteration can be explicit ( we will write the loop ) or implicit( using forEach method of Array class )

  

In matter of facts there is a way to override operator [] but for this our class must inherit Proxy class and we must implement all methods of the Array class. Moreover because this class will not extends Array class it will fail when we it check it with "is" for Array type ( is Array will return false ). More infomration about using a proxy class can be found on my website.

 

This function wil create an array with size - nSize and initialize all elements.

 

function fnc( nSize: int, type:* ): Array
{
// Creating an array with requested size - nSize.
// Creating an array with size (new Array( nSize )) is a faster way
// to create an array instead of using push method in loop nSize-times .
var arr: Array = new Array( nSize );
 // This is a faster way - one if and one loop itteraction
if( type is Class )
{
  // if the passed type is a class. The initializing is creation of an instance of this class for every element of the array
  for ( var iPos: uint = 0; iPos < nSize; ++iPos )
  {
   arr[ iPos ] = new type();
  }
}
else
{
  //type is value so assign this value on every element of the array
  for ( var iPos: uint = 0; iPos < nSize; ++iPos )
  {
   arr[ iPos ] = type;
  }
}
 return arr;
}

Test cases:

Creating an Array with three elements initialized with value 7;
var arr: Array = fnc( 3, 7 );
trace( arr ); //output 7,7,7

If you have a class:

class S
{
 public var t: int =7;
}




Creating an Array with 5 elements in this way:
var arr2: Array = fnc( 5, S );
will create 5 instances of the class S.
trace( arr2[ 3 ].t ) // output 7
 
Another way is to use an forEach method of the Array class like this

 
function initializeArray( arrToInit: Array, type: * )
{
 if ( type is Class )
 {
  arrToInit.forEach( initWithClass );
 }
 else
 {
  arrToInit.forEach( initWithValue );
 }
 
 
 function initWithClass(element:*, index:int, arr:Array):void
 {
   arr[ index ] = new type();
 }
 function initWithValue(element:*, index:int, arr:Array):void
 {
   arr[ index ] = type;
 }
}

 

 
Test case:
var arrTest:Array = new Array(3);
 
initializeArray( arrTest, 5 );
 
trace( arrTest[1] ); // output 5

 

+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes