A list is created by making a call to the List class object. Since there are probably not going to be any creation-time variables to set, the method used is typically create, rather than a createBegin/createEnd pair. In order to use method calls against the List object, one can import the header file collections.h from the Swarm library, and then this command will create a List object:
id nameOfSomeList; nameOfSomeList = [List create: self]; |
Recall from the discussion of object creation that self refers to the memory zone in which the list is created. This is appropriate in a Swarm or GUISwarm instance, while other classes would use [self getZone] in place of self.
Once a list object exists, it can carry out many instructions. In the List protocol, methods that can add and remove either the first or last object in a list are defined. For example, to add an object called fred at the end of a list called listOfDogs, one could write:
[listOfDogs addLast: fred]; |
and, if it were necessary to remove the last object in the list, one could write:
[listOfDogs removeLast]; |
The object of type List is able not only to carry out the addFirst:, addLast:, removeFirst:, and removeLast: methods, it can also inherits methods from the Collections protocol. Some of the useful methods in the Collections protocol are:
getCount: Use this to find out how many items are already in the list
begin: aZone: This creates an Index object that can be used to traverse this list.
remove: aMember. This will search through a list to find aMember and it will remove that object (and return it).
removeAll: This take all elements out of the list, but it will not destroy the list or the elements in the list.
deleteAll: Be careful: this removes the elements from memory as it clears the list
Lists have many uses in Swarm projects. The following sections discuss them, in turn. First, lists are used to manage collections of objects and schedule their activities in the model swarm layer of a simulation. Second, lists are used to pass information back and forth between levels of a simulation. Third, lists can be used by individual agents to keep track of their experiences and manage their information