PDA

View Full Version : Array


Tazbo
02-17-2009, 11:49 AM
Hi Guys,

I am really new to actionscripting and I am trying to understand some actionscript but I am a little confused. My code is

var myArray:Array = new Array("a", "b", "c");
myArray.push("d");
myArray.push("e");
myArray.push("f");

var i:int;
for (i = 0; i < (myArray.length); i++) {
trace(myArray[i]);
}


What I am confused about is how the following code is broken down.
I know var = variable and i must be the name of the variable but after that I am so confused. Please help thanks

var i:int;
for (i = 0; i < (myArray.length); i++) {
trace(myArray[i]);
}

Arif-sama
02-17-2009, 12:13 PM
"for i=0", "i" starting equaling from 0 to array.length (which in this case means 6) do something (in this case trace myArray[i]), i++ means to add +1 to the i value, i.e. if i = 1, then i++ will make its value 2;

myArray[i] means "i" indexed item of the array

myArray[5] means item of the array with the index 5

another way you can write it:
var i:Number = 5;
myArray[i] - this is also means item of the array with index number 5;
so since this process will repeat 6 times (because i<myArray.length, and myArray.length = 6), it will do it like this:

i=1;
trace(myArray[i]);
then it will check if i is less than myArray length, if it is less, then it will add +1 to i, and will repeat the process. so it will be
i=2
trace(myArray[i]);
and so on

tadster
02-17-2009, 05:19 PM
i starts at 0, i should not be more than the length of the array, i will be added by one.
(initialize) (limit) (increment)

And all of that is a loop.
What is in {} will happen for as long as it takes i to reach the limit.

It's called a for loop.

Saying myarray[i] produces the array piece that corresponds to the number that i currently equals during the loop.

Since myarray has 6 parts (0-5), i will start at 0 and get added by one until it is five,
and all the pieces of the array will get displayed in the trace.

"a" is the 0 (first) part of the array "b" is 1 "c" is 2 and so on...

hope this helps

oh yea, just in case you didn't realize it "i" can be called anything
you could call it "pleasentlearning" or "j" or "bobalooga" or whatever you want
it is a variable

the word var signifies a new variable