(sortOn) not added
Array
An array is a group of things.
Back to our concept of a movie script needing actors, an Array is a group of actors. Instead of calling one actor individually you can section the actors off into to groups and then call the whole group. Then, each actor in the group performs his role.
Examples:
var arrayMan1: Array=[“I am arrayMan”];
var arrayMan2: Array= new Array();
Here is a more elaborate example:
var arrayMan: Array= new Array("Blast!!","zaaaap");
trace(arrayMan1);
trace(arrayMan2);
What will show is:
I am arrayMan
Blast!!,zaaaap
Arrayman plays the role of an Array, which is a group of different roles (he has more than one role he can play). And as of now he is a “new Array” Array.
All you have to do now is put some stuff into the new Array. Since this variable is ‘arrayman,’ I put some blast power and zap power into it. Then I traced arrayman to see what showed up (‘trace’ is a pre-made function).
Look at this other Array:
var mrsArray: Array= new Array("I am Mrs. Array","hello");
trace(mrsArray);
This returns:
I am Mrs. Array,hello
Here is another way to call an Array
trace(arrayman[1]);
//instead of having to trace all things, you can trace one particular thing
//each item of an array is numbered from zero up.
//"zaaap" is item 1 of arrayMan and "Blast!!" is item 0 of arrayMan
This traces:
zaaaap
You can also change the numbering order of the Array:
var mrsarray: Array= new Array("I am Mrs. Array","hello");
mrsArray[0]="hello"
trace(mrsArray[0]);
This traces:
hello
(Notice that “I am Mrs. Array” was the original [0] item.)
Another way to tweak the array is to trace something not even in the array as a numbered item in the array:
var arrayOfGoldenSun: Array= new Array();
("Do","Re","Me","Fa","So","La","Tee","Do");
arrayOfGoldenSun[1]="not in the song";
trace(arrayofgoldensun);
This traces:
Do,not in the song,Me,Fa,So,La,Tee,Do
Property length of the Array Class
You can see how long an array is by:
trace(arrayOfGoldenSun.length);
This returns:
8
This means there are 8 items in the arrayofgoldensun Array, including the 0 item.
I could even trace the length of the Array minus any number by:
trace(arrayofgoldensun.length-1);
This returns:
7
Method push of the Array Class
I can add stuff to the array by pushing something in (think of shoving an actor into a group at the last minute before the director starts rolling the film). A push looks like this:
arrayOfGoldenSun.push("Re");
trace(arrayOfGoldenSun);
This returns:
Do,not in the song,Me,Fa,So,La,Tee,Do,Re
I added the “Re” back to arrayOfGoldenSun
Method reverse of the Array Class
You can reverse the order of an Array like this:
arrayOfGoldenSun.reverse();
trace(arrayOfGoldenSun);
You get:
Re,Do,Tee,La,So,Fa,Me,not in the song,Do
Check out the order now:
trace(arrayofgoldensun[0]);
It returns:
Re
Arrays can contain more than just strings.
var arrayser: Array= new Array(1,2,"skip a few",99,100,Boolean);
trace(arrayser);
This traces:
1,2,skip a few,99,100,false
Note: numbers in an Array don‘t have to be in quotation marks.
EXTRA INFORMATION ON ARRAYS
A list of things you can do to Arrays
Note: These are methods:
concat
join
pop
push
reverse
shift
slice
sort
sortOn
splice
toString
unshift
Array.concat() links the parameters and returns them as a new array. Concat is short for concatenate.
Array.join() Joins all elements of an array into a string.
Array.pop() Removes the last element of an array and returns its value.
Array.push() Adds one or more elements to the end of an array and returns the array’s new length.
Array.reverse() Reverses the direction of an array.
Array.shift() Removes the first element from an array and returns its value.
Array.slice() Extracts a section of an array and returns it as a new array.
Array.sort() Sorts an array in place. In other words it makes it alphabetical or chronological.
Array.sortOn() Sorts an array according to a field in the array.
Array.splice() Adds and removes elements from an array.
Array.toString() Returns a string value representing the elements in the Array object.
Array.unshift() Adds one or more elements to the beginning of an array and returns the array’s new length
This is how they are used:
Array.concat()
EXAMPLE 1
var alphaArray:Array = new Array("a","b","c");
var numericArray:Array = new Array(1,2,3);
var alphaNumeric_array:Array = alpha_array.concat(numeric_array);
trace(alphaNumericArray);
// creates array [a,b,c,1,2,3]
EXAMPLE 2
var aArray:Array = new Array ("a","b","c");
// 2 and 3 are elements in a nested array
var nArray:Array = new Array(1, [2, 3], 4);
var xArray:Array = aArray.concat(nArray);
trace(xArray[0]); // a trace(xArray[1]);
// b
trace(xArray[2]); // c trace(xArray[3]);
// 1
trace(xArray[4]); // 2, 3 trace(xArray[5]);
// 4
Array.join()
EXAMPLE1
var aArray:Array = new Array("Earth","Moon","Sun")
trace(aArray.join());
// displays Earth,Moon,Sun
trace(aArray.join(" - "));
// displays Earth - Moon - Sun
trace(aArray.join(" + "));
// displays Earth + Moon + Sun
EXAMPLE 2
var aNestedArray:Array = new Array(["Europa", "Io", "Callisto"], ["Titan", "Rhea"]);
trace(aNestedArray.join(" + "));
// returns Europa,Io,Callisto + Titan,Rhea
Array.length
EXAMPLE1
var myArray:Array = new Array();
trace(myArray.length);
// initial length is 0 myArray[0] = 'a';
trace(myArray.length);
// myArray.length is updated to 1
myArray[1] = 'b'; trace(myArray.length);
// myArray.length is updated to 2 myArray[9] = 'c';
trace(myArray.length);
// myArray.length is updated to 10
trace(myArray);
// displays:
//a,b,undefined,undefined,undefined,undefined,undefined,undefined,undefined,c
// if the length property is now set to 5, the array will be truncated
myArray.length = 5; trace(myArray.length);
// myArray.length is updated to 5
trace(myArray);
// outputs: a,b,undefined,undefined,undefined
Array.pop();
EXAMPLE1
var myPetsArray:Array = new Array("cat", "dog", "bird", "fish");
var popped:String = myPetsArray.pop();
trace(popped);
// displays: fish
trace(myPetsArray);
// displays: cat,dog,bird
Array.push();
EXAMPLE1
var myPetsArray:Array = new Array("cat", "dog");
var pushed:Number = myPetsArray.push("bird", "fish");
trace(pushed);
// displays 4
Array.reverse();
EXAMPLE1
var numbersArray:Array = new Array(1, 2, 3, 4, 5, 6);
trace(numbersArray);
// displays 1,2,3,4,5,6
numbersArray.reverse();
trace(numbersArray);
// displays 6,5,4,3,2,1
Array.shift();
EXAMPLE1
var myPetsArray:Array = new Array("cat", "dog", "bird", "fish");
var shifted:String = myPetsArray.shift();
trace(shifted); // displays "cat"
trace(myPetsArray);
// displays dog,bird,fish
Array.slice();
EXAMPLE1
var myPetsArray:Array = new Array("cat", "dog", "fish", "canary", "parrot");
var myFourLeggedPetsArray:Array = new Array();
var myFourLeggedPetsArray = myPetsArray.slice(0, 2);
trace(myFourLeggedPetsArray);
// returns cat,dog
trace(myPetsArray);
// returns cat,dog,fish,canary,parrot
EXAMPLE2
var myPetsArray:Array = new Array("cat", "dog", "fish", "canary", "parrot");
var myFlyingPetsArray:Array = myPetsArray.slice(-2);
trace(myFlyingPetsArray);
// traces canary,parrot
EXAMPLE3
var myPetsArray:Array = new Array("cat", "dog", "fish", "canary", "parrot");
var myAquaticPetsArray:Array = myPetsArray.slice(2,-2);
trace(myAquaticPetsArray);
// returns fish
Array.sort();
EXAMPLE1
var fruitsArray:Array = new Array("oranges", "apples", "strawberries", "pineapples", "cherries");
trace(fruitsArray);
// displays oranges,apples,strawberries,pineapples,cherries
fruitsArray.sort();
trace(fruitsArray);
// displays apples,cherries,oranges,pineapples,strawberries
fruitsArray.sort(Array.DESCENDING);
trace(fruitsArray);
// displays strawberries,pineapples,oranges,cherries,apples
Note: ’DESCENDING’ is a property
EXAMPLE2
Note: The following example uses Array.sort() with a compare function:
var passwords_array:Array = new Array("mom:glam", "ana:ring", "jay:mag", "anne:home", "regina:silly");
function order(a, b):Number
{
//Entries to be sorted are in form name:password
//Sort using only the name part of the entry as a key.
var name1:String = a.split(":")[0];
var name2:String = b.split(":")[0];
if (name1<name2) { return -1; }
else if (name1>name2)
{ return 1; }
else { return 0; }
}
trace("Unsorted:");
//displays Unsorted:
trace(passwords_array);
//displays mom:glam,ana:ring,jay:mag,anne:home,regina:silly
passwords_array.sort(order);
trace("Sorted:");
//displays: Sorted:
trace(passwords_array);
//displays ana:ring,anne:home,jay:mag,mom:glam,regina:silly
Array.sortOn();
EXAMPLE1
I have yet to understand this
Array.splice();
EXAMPLE1
var myPets_array:Array = new Array("cat", "dog", "bird", "fish");
trace( myPets_array.splice(1) );
// dog,bird,fish
trace( myPets_array );
// cat
Note: A cut is made at ‘dog,’ and ‘cat’ is left
Array.toString();
EXAMPLE1
my_array:Array = new Array();
my_array[0] = 1;
my_array[1] = 2;
my_array[2] = 3;
my_array[3] = 4;
my_array[4] = 5;
trace(my_array.toString());
// displays 1,2,3,4,5
Array.unshift();
EXAMPLE1
var pets_array:Array = new Array("dog", "cat", "fish");
trace( pets_array );
// dog,cat,fish pets_array.unshift("ferrets", "gophers", "engineers");
trace( pets_array );
// ferrets,gophers,engineers,dog,cat,fish
Resources for some information listed can be found in the Resource.pdf file.