Skip to main content
Inspiring
December 27, 2011
Answered

Arrays

  • December 27, 2011
  • 1 reply
  • 1073 views

hi

Being a late, late beginner, I would like some help to visualize the following operation:

Let' say I have 5 numbers into an array, I would like to pop each of them one by one out of the array and multiply them by 5 every time. Thank you for helping.

var myArray:Array = ["1", "2", "3","4","5"];

This topic has been closed for replies.
Correct answer Rothrock

Thank you for your answer. Appreciate every input. I want to wish you a happy new year.

Tell me, what would be the practical use of " pop" ?


The array class has many different methods. Another of which is push() which adds values to the end of the array. Using push and pop it is possible to easily create a last-in-first-out type of system. For example supposed you wanted to make a navigation similar to a browser back button.

Each time you visit a new page/location/etc. you push that location onto your array of locations. Then when you want to back up you pop off the last one.

It is similar to a last-in-first-out kind of system like a desktop inbox might have.

There are other methods for the array class as well. Like unshift(). Check the documentation. Arrays are really quite useful. Many of their operations are "expensive" in terms of processing time. But for most basic stuff they work just great.

1 reply

SamsimmsAuthor
Inspiring
December 27, 2011

I think the array should be without the quotes.

var myArray:Array = [1, 2, 3,4,5];

I can trace the first one, and then what ? I get 1.  Is it  myArray[0]*(5)

  

var myArray:Array = [1,2,3,4,5];

trace(myArray[0]);

SamsimmsAuthor
Inspiring
December 27, 2011

I did some reading, & I think I got it:

myArray[0]*(5);

tF.text=String(myArray[2]*(5));

It gave me 15.

Inspiring
December 27, 2011

Yup. That is how you access elements in an array -- using the [ ] array access operator. There is a little bit more you should probably know....

In your OP you mention "pop" which is actually a method (function) of the Array class. Try this and see:

var myArray:Array=[1,2,3,4,5];

trace("Original array: "+myArray);

var val:Number=myArray.pop();

trace("Value: "+val);

trace("Now look at my array: "+myArray);

And then try it with myArray.shift();

These are different ways of working with arrays.