Home | Flash AS3 Tutorials | Flash Animation Tutorials | Store

Access Last Element of Array with Flash ActionScript 3

This Flash ActionScript tutorial shows how to access the last element of Array.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

We always require to know the last element or last value of an Array. This Flash ActionScript tutorial will show you how to access the last value of an Array.

The Flash Movie of this tutorial is shown as above.

Flash ActionScript Codes:

//Create an empty Array with the name trayArray
//You may imagine an Array like a DIY ice cube tray
//You don't need to decide the size at this moment
//You can assembly any number of ice cavity for your ice cube tray later
var trayArray:Array = new Array();

 

//We are going to make some ice cubes with different tastes
//Five different juices (values) are put into the Array
//Therefore the size of this Array is now 5
//Since this is a DIY ice cub tray, the size can be changed anytime.
trayArray = ["papaya juice", "orange juice", "apple juice", "milk", "coke"];

 

//Display all values of trayArray
var i:int;
for (i = 0; i < trayArray.length; i++) {

this["item" + (i + 1) + "_txt"].text = trayArray[i];

}

 

//Get the value of last element of testArray no matter
//what is the size of this Array
//The index of last element of this Array is always trayArray.length -1
output_txt.text = "The value of last element of trayArray is: " + trayArray[trayArray.length -1];

Download Flash Source File:

Flash Source File array-5.fla

Remarks:

This Flash ActionScript tutorial also shows how to access the last element of an Array. For some Flash games or Flash webpage design, we like to access a random element of an Array. The next tutorial will show you how.