|
Primer : Collections (Bags) (Part 1) |
|
|
The Collection class (and all its sub-classes) is one feature of the Smalltalk
language that makes it so powerful. Tedious lines of code that are written over
and over again in other languages are just a single message in Smalltalk. Sometimes,
the best way to learn about something is just to play with it. The Bag collection
is no exception.
|
||
|
This lesson introduces Bags and some of their specific characteristics.
|
||
|
1.
If VisualWorks is not already running, please start running it now.
2. From the main VisualWorks Launcher window, either click the last button on the Toolbar or select the menu option Tools>>Workspace. 3. In the Workspace, enter the following:
| myBag |
4.
Highlight all of this text, <Operate-Click> and select Do it.
myBag := Bag new. myBag add: 'dog'. myBag add: 'cat'. myBag inspect. Note that a new (inspector) window will appear and the caption of the window is a Bag. 5. In the left pane of the Inspector window, click (highlight) the word self. You should see Bag ('dog' 'cat') 6. In the left pane of the Inspector window, click (highlight) the word contents. You should see Dictionary ('dog'->1 'cat'->1 )
7. Close the Inspector window |
||
|
How does a Bag (Collection) work?
By definition, a "bag" is very much like its physical counterpart - a thing that holds whatever you throw into it or whatever you decide to collect. For whatever reason, your own reason, the items you place in the bag all have something in common (i.e something similar about the group). In this case, it is a group of small animals. It stores whatever you place in it yet one nice feature of a bag is that it counts "like" items. Watch what happens when you compare the results (above) to the one below. |
||
|
8.
In the Workspace, enter the following:
| myBag |
9.
Highlight all of this text, <Operate-Click> and select Do it.
myBag := Bag new. myBag add: 'dog'. myBag add: 'cat'. myBag add: 'dog'. myBag add: 'cat'. myBag add: 'dog'. myBag inspect. Note that a new (Inspector) window will appear and the caption of the window is a Bag. 10. In the left pane of the Inspector window, click (highlight) the word self. You should see Bag ('dog' 'dog' 'dog' 'cat' 'cat') 11. In the left pane of the Inspector window, click (highlight) the word contents. You should see Dictionary ('dog'->3 'cat'->2 )
12. Close the Inspector window |
||
|
Can you see how a Bag works now?
On the first line, we declared a temporary variable (myBag). On the second line, we told Smalltalk to create a new Bag and assign it to our temporary variable. On the third line, we added the String 'dog' to our bag. On the next line, we added the String 'cat' to our bag. We did this twice more for 'dog' and once more for 'cat'. Then we told Smalltalk to inspect our bag. When we executed the code to add 'dog' for the second time, Smalltalk looked inside the bag, determined that 'dog' was already there, incremented the count for 'dog' and then placed 'dog' in the bag. In most programming languages, you would have to write this code yourself. In Smalltalk, it's built into the language. | ||
|
SummaryBags are collections of any type of items but it keeps a count of how many of a particular item is in the bag. Although there are many methods associated with bags, at this time we are only concerned about how to add items to a bag and how to display the count of items in a bag. |