Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an array with a single element in it?

I am trying to define an array with single element... so,

var arr:Array = new Array(1,2,3,4) // arr[0] = 1 
 // but
var arr:Array = new Array(1) // arr[0] = undefined 

//Also, 

var arr:Array = new Array([1]) // arr[0] = 1 , << ILLUSION
//Because, arr[0] is NOT A NUMBER, IT ITSELF IS OF TYPE=> ARRAY. 

var arr:Array = [1] //arr[0]=1 but i think it's AS1.0 notation..

So, is their any AS3.0 way of defining array with single element ?

like image 979
Vishwas Avatar asked Dec 05 '22 18:12

Vishwas


2 Answers

var arr:Array = [1]; //arr[0]=1 but i think it's AS1.0 notation..

Why? This is perfectly legal shorthand array initialization, and it's exactly the way to do it.

like image 94
weltraumpirat Avatar answered Dec 31 '22 07:12

weltraumpirat


Lol, I remember dealing with this a year or 2 back, the way I did it was with 2 lines.

var arr:Array = new Array();
arr[0] = "the element";

This is because the constructor for Array accepts the size of the array as an argument if you are passing a single integer value. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#Array()

like image 23
ToddBFisher Avatar answered Dec 31 '22 07:12

ToddBFisher