29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

provides <strong>an</strong>y dimension size but requires additional programmer support. For example, in C to create a<br />

two dimensional array of 3 by 4 integer elements we could write:<br />

int table [3] [4];<br />

In <strong>Smalltalk</strong> we c<strong>an</strong> either use a TwoDList or create the two dimensional structure m<strong>an</strong>ually. For<br />

example, if we used the TwoDList class:<br />

| aTwoDList |<br />

aTwoDList := TwoDList columns: 3 rows: 4.<br />

We could then place the string ‘John’ into position 1, 1 using the atPoint:put: message. We c<strong>an</strong> then<br />

retrieve this string using the atPoint: message. Note these messages take a point object as the position. A<br />

point object c<strong>an</strong> be created using the @ form. For example:<br />

aTwoDList atPoint: (1 @ 1) put: 'John'.<br />

Tr<strong>an</strong>script show: (aTwoDList atPoint: (1 @ 1)).<br />

As this class possesses list like behaviour it c<strong>an</strong> be used in a similar m<strong>an</strong>ner to <strong>an</strong> inst<strong>an</strong>ce of list. For<br />

this reason it is often used with the user interface facilities provided in VisualWorks. For example, with<br />

the table widget. However, we c<strong>an</strong>not ch<strong>an</strong>ge the size of a TwoDList once it is created. Nor do we have<br />

the ability to use higher dimensions. It is sometimes therefore necessary to m<strong>an</strong>ually construct multi -<br />

dimensional arrays. For example:<br />

temp := Array new: 3.<br />

temp at: 1 put (Array new: 4).<br />

temp at: 2 put (Array new: 4).<br />

temp at: 3 put (Array new: 4).<br />

We could then access elements of the object in the following m<strong>an</strong>ner:<br />

((temp at: 1) at: 1)<br />

If you find yourself having to do this often then it probably me<strong>an</strong>s that you are thinking to<br />

procedurally <strong><strong>an</strong>d</strong> need to reconsider your design.<br />

o<br />

11.7 Dictionaries<br />

A Dictionary is a set of Associations, each representing a key -value pair. It is a subclass of<br />

Set. The elements in a Dictionary are unordered, but each has a definite name or key. Thus a<br />

Dictionary c<strong>an</strong> be regarded as <strong>an</strong> unordered collection of object values with external keys. Part of<br />

the protocol for dictionaries is listed below:<br />

• at: aKey returns the value associated with aKey.<br />

• at: aKey put: aValue puts aValue into a dictionary with the external key aKey.<br />

• associationAt: aKey <strong>an</strong>swers with the association given by the key (i.e. the value <strong><strong>an</strong>d</strong><br />

the key).<br />

• keyAtValue: aValue <strong>an</strong>swers with the name (key) associated with aValue.<br />

• keys <strong>an</strong>swers with a Set of keys from the receiver.<br />

• values <strong>an</strong>swers with a Bag of the values in the receiver. Note that values are not necessarily<br />

unique (hence they are returned as a Bag rather th<strong>an</strong> as a Set).<br />

Here is a simple Dictionary example you might like to type in <strong><strong>an</strong>d</strong> try out.<br />

| x |<br />

x := Dictionary new.<br />

x at: 'John' put: 'jjh'.<br />

x at: 'Myra' put: 'msw'.<br />

x at: 'Chris' put: 'cjr'.<br />

x at: 'Denise' put: 'dec'.<br />

Tr<strong>an</strong>script show: (x at: 'Chris') printString.<br />

Tr<strong>an</strong>script show: (x keyAtValue: 'jjh') printString.<br />

98

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!