The primary reason for using collections is to store data for future retrieval. This section of the guide focuses on how to add data with several of the primary collections. For more information on other data structures please refer to the Collection Classes section of the user guide.
asdk.ArrayList
is the primary class used for storing items in a list. Below is an example of storing items in
an ArrayList:
1 2 3 4 |
var groceryCart:asdk.Collection = new asdk.ArrayList(); |
The addItem
method can take any variable type as an argument. The example above demonstrates adding a several
strings to the list, but any type can be added. In addition note that an ArrayList can hold duplicate objects.
The asdk.HashMap
class is used for storing key value pairs and has the benefit of allowing for fast access to
stored data. Below is an example of storing items in a HashMap:
1 2 3 4 5 6 7 8 |
var id1 = 1; |
Using a HashMap, the put
method allows for adding objects based on a key. In the example above
several items are added to the map based on ids. This allows for subsequent fast lookups based on those ids so that "grapes" can
be retrieved without having to traverse all the elements in the collection.
The asdk.Stack
is a collection in which the last item stored is the first item retrieved (last in first out or LIFO).
This type of collection is ideal for simulating a deck of cards where the top card is always the next card that should be delt.
Below is an example of how to add items to the Stack class:
1 2 3 4 |
var cards:asdk.Stack = new asdk.Stack(); |
The push
method can take any variable type as an argument. The example above demonstrates adding a several
strings to the stack the represent cards.
The asdk.Queue
is a collection in which the first item stored is the first item retrieved (first in first out or FIFO).
Below is an example of how to add items to the Queue class:
1 2 3 4 |
var users:asdk.Queue = new asdk.Queue(); |
The push
method can take any variable type as an argument. The example above demonstrates adding a several
strings to the queue.
The asdk.LinkedList
is a collection in which element maintains a reference to the element before it and
the element after it. This kinds of data structure is helpful when needing to traverse both forward and backward within
a collection without concern for an elements position. Below is an example of how to add items to a LinkedList:
1 2 3 4 5 6 7 |
var rainbowColors:LinkedList = new LinkedList(); |
Similar to an ArrayList, a objects are added toa LinkedList via the addItem
method.