After storing data within a collection, it will subsequently need to be retrieved based on some change in state of the application. This section of the guide builds on the previous examples and demonstrates how to retrieve data from two of the primary collections.
Continuing with the previous example the code below demonstrates how to retrieve data from the groceryCart variable which is an ArrayList:
1 2 3 4 5 6 |
... |
This example traverses all of the elements within the ArrayList based on an index and the The code below demonstrated how to retrieve the data that was stored in the HashMap in the previous 'Storing Data' section
of the guide:
The The previous Stack examples stored three values that represented cards; continuting with this example, the code snippet
below demonstrates how the cards are retrieved from the stack.
The The example below demonstrates how to retrieve data from a queue. The API is exactly the same as the Stack, but in
this case the items are returned in the order they were added.
The Lastly, the example below demonstrates how to retreive data from the LinkedList created in the previous example.
One way to retrieve data from a LinkedList is to get the first item from the list and the continue querying the list
for the next item, until the next item that is returned is undefined signifying the end has been reached. The example
above does this by calling the Running the example above yields the following lines in the output window of the Flash IDE
The next section in the guide, Iterators, another technique for accessing data within a collection.
HashMap
1
2
3
4
5
6
7
8
9
10
11
12
13
var id1 = 1;
var id2 = 2;
var id3 = 3;
...
var apple = cache.get( id1 );
var orange = cache.get( id2 );
var grapes = cache.get( id3 );
trace( apple );
trace( orange );
trace( grapes );
get
method above takes a key as an input parameter and returns the value that is associated with the key. If
no value is associated with the key that has been passed to the get method, undefined (null) will be be returned. In this example
the three values associated with id1, id2, and id3 are returned.
Stack
1
2
3
4
5
6
7
8
9
10
var cards:asdk.Stack = new asdk.Stack();
cards.push("spades:ace");
cards.push("hearts:queen");
cards.push("clubs:two");
trace( "top card = " + cards.pop() );
//displays top card = clubs:two in the output window
trace( "next card = " + cards.pop() );
//displays next card = hearts:queen in the output window
trace( "bottom card = " + cards.pop() );
//displays bottom card = spades:ace in the output window
pop
method returns the object that is on the top of the Stack. In this example it returns the "clubs:two"
string first, the "hearts:queen" string second, and the "spades:ace" string last, honoring a LIFO contract.
Queue
1
2
3
4
5
6
7
8
9
10
var users:asdk.Queue = new asdk.Queue();
users.push("first in line");
users.push("second in line");
users.push("third in line");
trace( users.pop() );
//displays first in line in the output window
trace( users.pop() );
//displays second in line in the output window
trace( users.pop() );
//displays third in line in the output window
pop
method here returns items in a first in first out ordering so that "first in line" prints out first,
"second in line" prints out second, and "third in line" prints out last.
LinkedList
1
2
3
4
5
6
7
8
9
10
11
12
13
14
var rainbowColors:LinkedList = new LinkedList();
rainbowColors.addItem( "purple" );
rainbowColors.addItem( "blue" );
rainbowColors.addItem( "green" );
rainbowColors.addItem( "yellow" );
rainbowColors.addItem( "orange" );
rainbowColors.addItem( "red" );
var color = rainbowColors.getFirst();
do
{
trace( color.getData() );
color = color.next;
} while( color != undefined )
getFirst
method of the LinkedList object (line 8 ) and then using the do
while construct, continually updating the color object with the next
LinkedListItem (line 13). It
should be noted that the color object that is returned from the getFirst
method and the next
property is an instance of
LinkedListItem, not the original object added. Therefore it is necessary to call the getData method (line 7) to retrieve
the object originally stored.
1
2
3
4
5
6
purple
blue
green
yellow
orange
red