0

Is there any way to create instances of array in a for loop?

Here's my code...

var recArrCon1:Array = new Array(50);
var recArrCon2:Array = new Array(50);
var recArrCon3:Array = new Array(50);
var recArrCon4:Array = new Array(50);
var recArrCon5:Array = new Array(50);
var recArrCon6:Array = new Array(50);
var recArrCon7:Array = new Array(50);
var recArrCon8:Array = new Array(50);

I want to make declaration in a dynamic way by a for loop. Thanks in advance.

By the way, I'm using AS3

Edit: The answer is (from Barış Uşaklı):

var recArrCons:Object = {};
for(var i:int=1; i<=8; i++) 
{
 recArrCons["recArrCon" + i] = new Array(50);
}
trace(recArrCons.recArrCon4); // 4th array
asked May 15, 2013 at 1:34
1
  • What language are we talking about? Commented May 15, 2013 at 1:36

2 Answers 2

2

Make the class containing this code dynamic then you can create the names dynamically.

for(var i:int=1; i<=8; i++) 
{
 this["recArrCon" + i] = new Array(50);
}
trace(this.recArrCon4); // 4th array

Or you can store them in an Object like :

var recArrCons:Object = {};
for(var i:int=1; i<=8; i++) 
{
 recArrCons["recArrCon" + i] = new Array(50);
}
trace(recArrCons.recArrCon4); // 4th array
answered May 15, 2013 at 2:14

5 Comments

Edited my answer, it will only work if the containing class is dynamic
Thank you sir... This is the best and cleanest way I can derive new instances of Array bu storing it to an Object. Thanks a lot.
Sir, how about accessing each object.. I'm trying to insert value on those arrays with dynamic variable String added in name like this... var firstPName:String = "2"; for(var i:int=1; i <= 50; i++){ if(!this["recArrCons.recArrCon"+firstPName][i]){ this["recArrCons.recArrCon"+firstPName][i] = secondPName; break; } } It receives error on runtime. Though, I can access the values by directly calling them according to their names without "this[]".
recArrCons['recArrCon'+ firstPName][i] will give the ith element. It will be much more readable if you store a reference to the array though like : var arr:Array = recArrCons['recArrCon'+firstPName] and ten arr[i]
okay sir.. thanks alot. I now understand it that way... recArrCons['recArrCon'+ firstPName][i] ... I'll try this first.
0

I wouldn't create instances of Array the way you are, it's very messy. I suggest using a list of arrays like this:

var arrays:Vector.<Array> = new <Array>[];
for(var i = 0; i < 8; i++)
{
 arrays.push(new Array(50));
}

Where you would access an array like this:

var inner:Array = arrays[2];

And values of the arrays using [x][y]:

trace(arrays[0][0]);
answered May 15, 2013 at 2:33

2 Comments

it's a 2 dimensional array. I have think this way too. But, base on the need of my application, it may cause some problem. By the way Sir Marty. Thank you for response with my question. I'll get to use it someday.
@flashMark Sounds fair, just be cautious of polluting the current scope with a bunch of arrays.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.