Next: SortedList, Previous: List, Up: Classes [Index]
AssociativeArray
ClassObjects of class AssociativeArray
contain members that are
stored and retrieved using Key
objects. See Key.
AssociativeArray
objects use the atPut
and at
methods
in Collection
class to store and retrieve objects. See Collection.
The keyAt
method returns the key/value pair of the array element named
as its argument.
The map
method maps over each object stored in an
AssociativeArray
, and provides that element as the receiver to
the method or code block given as the argument.
If you want to work with the object stored in the array and the key it
is stored with, the mapKeys
method iterates over each key/value
pair of the AssociativeArray
The following example shows how to retreive the keys and values stored
in an AssociativeArray
. Each value that is stored in the array
is a String
object, so the program does not need to check for
different classes of objects that are stored in the array.
AssociativeArray instanceMethod printValue (void) { printf ("%s\t", self name); printf ("%s\n", self value); return NULL; } AssociativeArray instanceMethod printKeyValue (void) { String new valueObject; printf ("%s =>\t", self name); valueObject = self getValue; printf ("%s\n", valueObject); return NULL; } int main () { AssociativeArray new assocArray; String new s1; String new s2; String new s3; WriteFileStream classInit; s1 = "string1 value"; s2 = "string2 value"; s3 = "string3 value"; assocArray atPut "string1", s1; assocArray atPut "string2", s2; assocArray atPut "string3", s3; stdoutStream printOn ("%s\n%s\n%s\n\n", (assocArray at "string1"), (assocArray at "string2"), (assocArray at "string3")); stdoutStream printOn "%s\n%s\n%s\n\n", (assocArray at "string1"), (assocArray at "string2"), (assocArray at "string3"); stdoutStream printOn "%s\n%s\n%s\n\n", assocArray at "string1", assocArray at "string2", assocArray at "string3"; assocArray map printValue; assocArray mapKeys printKeyValue; return 0; }
at
(char *
key)
Retrieve the element of the receiver array stored at key.
init
(...
)
=
(...
)
Append the arguments to the receiver AssociativeArray
.
The argument list may be any number of key,value pairs.
Here is an example.
myAssocArray init "key1", "first", "key2", "second", "key3", "third", "key4", "fourth"; myAssocArray append "key5", "fifth", "key6", "sixth", "key7", "seventh", "key8", "eigth"; ... or ... myAssocArray = "key1", "first", "key2", "second", "key3", "third", "key4", "fourth"; myAssocArray += "key5", "fifth", "key6", "sixth", "key7", "seventh", "key8", "eigth";
atPut
(char *
key, OBJECT *
item)
Add item to the receiver at key.
init
(...
)
=
(...
)
Initializes the receiver AssociativeArray
to the values given
as the arguments, which may be composed of any number of
key,value pairs. Here is an example.
myAssocArray init "key1", "value1", key2", "value2", "key3", "value3"; ... or ... myAssocArray = "key1", "value1", key2", "value2", "key3", "value3";
keyAt
(String
keyName)
Returns the Key
object named by keyName.
map
(OBJECT *(*
method)()
)
Execute method, an instance method of class
AssociativeArray,
for each member of the receiver array. Each
time method is executed, self
refers to the object stored
in the associativeArray. This method returns NULL.
setValue
(void
)
A wrapper method for getValue
in class Key.
If received
by an instance of Collection
or its subclasses instead of a
Key
object, this method generates an exception.
Next: SortedList, Previous: List, Up: Classes [Index]