Latest topics
» GUYS. "DOOD" IS HERE!
Tue Nov 29, 2011 10:27 am by Karma

» Hi guys! Lalala
Mon Nov 28, 2011 9:26 pm by Pukito

» ARGHHHrrhhghghg
Sun Nov 27, 2011 1:25 pm by Pukito

» [Flash Game] - I don't even
Sat Nov 26, 2011 11:08 pm by Karma

» Hi, nice to meet you.
Sat Nov 26, 2011 12:48 am by Pukito

» I think I see ..
Sat Nov 26, 2011 12:47 am by Pukito

» Where Karma goes...
Fri Nov 25, 2011 2:14 am by Karma

» Guys
Tue Nov 22, 2011 9:11 pm by Pukito

» Karmini's Pose
Tue Nov 22, 2011 8:31 pm by Karma

» The Order (1)
Sat Nov 19, 2011 10:42 pm by Karma

May 2018
SunMonTueWedThuFriSat
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


[AS3 Chapter 3] Array Basics

Go down

[AS3 Chapter 3] Array Basics

Post by Pukito on Sun Oct 09, 2011 6:04 pm

Basics

Previously, I had made a post regarding variables. I had intended to talk about arrays and objects in that post as well, but it seems that I have forgotten. But that's okay Cool because arrays and objects are special and deserve more love. clown They will be very useful later on in life...so, here it is:

What is an array?
An array is basically a list, or a matrix that holds multiple values. An array is denoted by the square brackets [] and each element (value in the array) is placed inside of those brackets and separated by commas.

[0,9,3,2] This is an array which is holding four separate values 0, 9 3
and 2. Notice that each element in the array is separated by a comma (,). Arrays can
also be made using strings, or a combination of both strings and
numbers:

[0, "dog", 5, "cat", 243.3, -2] This is a perfectly valid array.

Basically, anything that can be held in a variable can be held in an array. This includes Flash objects such as symbol instances. (because these can be stored in variables)

Arrays can also hold other arrays:
[ [0,2,5],
["horse", "dog", "chicken"],
[4, "beaver", -4] ]

This is called a multidimensional array.
Notice that each distinct array is separated by a comma.

Declaring An Array
Declaring an array is much like declaring any other variable. You use the 'var' keyword.

Code:

var array:Array = new Array(); //declares a new array called array

Remember the trace function? That can be used to tell you what is being stored inside of an array. If we used trace(); for the array that we just declared above, then there would be no output because the array is empty. So how can we fill the array?

If you want to add values to an array while declaring it, you can place the elements within the brackets of Array();.

Code:

var dogs:Array = new Array("Whiskers","Ralph","Delphine");
trace(dogs); //output will be Whiskers,Ralph,Delphine

You can also type in the square brackets and add the elements that you want within the square brackets, separated by commas:

Code:

var array:Array = new Array(); //create a new array
array = [1, 2, 3, 4]; //add elements 1, 2, 3 and 4 to array
trace(array); //output will be 1,2,3,4

Both methods do the same thing, but it depends when you want the information added into the array. Method 1 adds the elements during the creation of the array and Method 2 adds the elements at any specific time that you want.

You can apply the same methods to multidimensional arrays as well:
Code:

var multidimensional:Array = (
[2,3,4,5,6,7,9,3],
[4,6,34,23,34,0,34,2],
);

//or

var multidimensional:Array = new Array();
multidimensional = [[2,3,4,5,6,7,9,3], [4,6,34,23,34,0,34,2]];

Indexes

Now, what if you wanted to access the information in the array? If there are multiple elements, how can you obtain a specific element? Well, it's quite easy actually. There are things called indexes on every array, which are basically labels for each element in the array. by default, the indexes are numeric and start from 0. Here's an example:

Code:

var cows:Array = ["Bethany","Beatrice","Milky"];

trace(cows[0]); //output will be Bethany
trace(cows[1]); //output will be Beatrice
trace(cows[2]); //output will be Milky

We could also use indices to add information in the array. Say that we had the following array:
Code:

var farm:Array = new Array("Pig","Bird","Cow","Horse");

and perhaps we want to replace the second element 'Bird' with 'Chicken'. Here's how this can be done:

Code:

farm[1] = "Chicken";

..and there you have it, the second element in the array was replaced using the index. It is very important that you remember that the first element of an array has an index of 0.

If you try to trace an index in an array that does not exist, it will say that it is undefined.
Code:

var array:Array = new Array(4,5,6);
trace(array[3]); //output will be undefined

Array Properties
To determine the numbers of elements, or the length of an array, the array length property can be used. This can be done by typing '.length' without the apostrophes after the array name.

Code:

var example:Array = [1,2,3];
trace(example.length); //output will be 3

Using the length value, you can also access the elements of an array.

Code:

trace(example.length-2); // output will be 2

That's because it accesses the the element with the index of length - 2. 3-1 = 1. It accesses the index of [1]. This is an important feature to remember, especially in longer arrays.

Array Push

Pushing an array means to add an element to the end of an array.

Code:

var dogs:Array = new Array("Pepper","Woof","Martha");

trace(dogs[3]); //output will be undefined

dogs.push("Karma"); //adds the element 'Karma' to the end of the array

trace(dogs[3]); //output will be Karma

Since an element is added to the array, an index is added as well because every element in the array must be associated with an index.

Array Pop and Splice

To remove elements from an array, you can use the array pop and splice methods. The array pop will remove the last element of an array, so it is the opposite of the array push method. The array splice method can be used to remove specific elements of an array.

Code:

var array:Array = new Array(1,2,3,4);

//the following methods can be used to remove the last element:

array.pop();
//or
array.splice(array.length-1);

Completely Clear An Array
To completely remove an array, all you need to do is set the array equal to 0. This can be achieved in a numerous amount of ways, but here are a couple:
Code:

var array:Array = new Array(1,2,3,4);
trace(array); //output will be 1,2,3,4

array.length = 0; //set array length to 0
trace(array); //output will be nothing, because the array is empty

//or
array = (1,2,3,4); //refill the array
trace(array); //output will be 1,2,3,4

array = []; //empty the array
trace(array); //output will be nothing. The array is empty


There are a gazillion other methods and properties about arrays, that this thread could go on forever, but these are just the array basics. Laughing Forgive me, I'm moving on to the next topic. Oh, I'll probably make a whole topic on multidimensional arrays later, because I barely talked about them here.

Just know that the methods and properties of arrays also apply to multidimensional arrays.


Last edited by Pukito on Sun Oct 09, 2011 8:45 pm; edited 1 time in total

_________________
Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig
avatar
Pukito
Cobbler

Posts : 377
Monies : 2669
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile http://www.areost.com

Back to top Go down

Re: [AS3 Chapter 3] Array Basics

Post by Karma on Sun Oct 09, 2011 7:09 pm

var smiles : Array = new Array ( " Very Happy ", " Suspect ", " alien ", ) ;

smiles . push (" What a Face ", ) ;

smiles[ 2 ] = " @ ",

smiles . push (" alien ",) ;

smiles [ 0 ] = " Exclamation ",

Yesh? No? D:


_________________
ppionngg


Pookie just likes to laugh at my life
avatar
Karma
Baker
Baker

Posts : 347
Monies : 2481
Reputation : 7
Join date : 2011-09-27
Age : 24
Location : Your face

View user profile

Back to top Go down

Re: [AS3 Chapter 3] Array Basics

Post by Pukito on Sun Oct 09, 2011 7:13 pm

Karma wrote:var smiles : Array = new Array ( " Very Happy ", " Suspect ", " alien ", ) ;

smiles . push (" What a Face ", ) ;

smiles[ 2 ] = " @ ",

smiles . push (" alien ",) ;

smiles [ 0 ] = " Exclamation ",

Yesh? No? D:


Yes. Very Happy
The comma after the last element is unnecessary clown. SOON YOU'LL BE PROGRAMMING WITH ME. AHAHAHAH

_________________
Put on your sunglasses... Cool and put on your 'fro... afro . Because I'm here now clown.

My friends list:

Rainy albino
Karma scratch
Serene pig
avatar
Pukito
Cobbler

Posts : 377
Monies : 2669
Reputation : 4
Join date : 2011-09-27
Location : MHMM.

View user profile http://www.areost.com

Back to top Go down

Re: [AS3 Chapter 3] Array Basics

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum