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.

11. Further Collection Classes<br />

11.1 <strong>Introduction</strong><br />

This chapter continues the discussion of the collection class hierarchy. It concentrates on the ordered<br />

collections <strong><strong>an</strong>d</strong> builds on the previous chapter. Sections two, three <strong>an</strong><br />

d four present the<br />

SortedCollection class, the List class <strong><strong>an</strong>d</strong> the Interval Class. The Array class,<br />

Dictionary class <strong><strong>an</strong>d</strong> String class are presented in Sections five to seven. Section eight describes<br />

how to iterate over collections while Section nine explai ns how to convert between collections. Figure<br />

11.1 illustrates the relationships between some of the ordered collection classes.<br />

Figure 11.1: Some of the ordered collection classes<br />

11.2 SortedCollection<br />

A SortedCollection is a collection class in which the order of the elements is determined by a<br />

specified sorting criteria (for example x i<br />

< x i+1<br />

). This is specified by the sort block associated with every<br />

sortedCollection object. That is, the ordering is defined by a special block of code (for example, [:x<br />

:y | x>= y]). This block c<strong>an</strong> be replaced by the user of the class so that <strong>an</strong>y type of ordering c<strong>an</strong> be<br />

applied to the objects with in the collection. Why not browse the class using the class browser. Try <strong><strong>an</strong>d</strong><br />

find out what type of sorting algorithm is being used.<br />

Try out the following in a Workspace:<br />

| x |<br />

x := SortedCollection new.<br />

x add: 5.<br />

x add: 6.<br />

x add: 7.<br />

x add: 1.<br />

x inspect.<br />

95

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

Saved successfully!

Ooh no, something went wrong!