size
, do:
, select:
, includes:
, collect:
...1
OrderedCollection
(dynamically growing)Array
(fixed size, direct access)Set
(no duplicates)Dictionary
(key-based, aka. maps)
Common messages work on all collections
with: anElt
, with:with:
, withAll: aCollection
size
, at: anIndex
, at: anIndex put: anElt
isEmpty
, includes: anElt
, contains: aBlock
, add: anElement
, addAll: aCollection
remove: anElt
, remove: anElt ifAbsent: aBlock
, removeAll: aCollection
do: aBlock
, collect: aBlock
, select: aBlock
, reject: aBlock
, detect: aBlock
, ...asBag
, asSet
, asOrderedCollection
, asSortedCollection
, asArray
new
instantiates one objectnew: size
creates an object specifying its sizeArray new: 4
> #(nil nil nil nil)
Array new: 2
> #(nil nil)
(OrderedCollection new: 1000)
OrderedCollection withAll: #(7 7 3 13)
> an OrderedCollection(7 7 3 13)
Set withAll: #(7 7 3 13)
> a Set( 7 3 13)
Remember: no duplicate in Sets
OrderedCollection new: 5 withAll: 'a'
> an OrderedCollection('a' 'a' 'a' 'a' 'a')
#('Calvin' 'hates' 'Suzie') at: 2
> 'hates'
#('Calvin' 'hates' 'Suzie') asOrderedCollection at: 2
> 'hates'
Collections can contain any sort of objects
#('calvin' (1 2 3))
> #('calvin' #(1 2 3))
| s |
s := Set new.
s add: Set new;
add: 1;
add: 2.
s asArray
> an Array(1 2 a Set())
do: aBlock
#('Calvin' 'hates' 'Suzie')
do: [ :each | Transcript show: each ; cr ]
at:
and at:put:
#( ... )
new:
#('Calvin' 'hates' 'Suzie') size
> 3
is equivalent to
((Array new: 3)
at: 1 put: 'Calvin';
at: 2 put: 'hates';
at: 3 put: 'Suzie') size
> 3
Getting the size of a collection
#('Calvin' 'hates' 'Suzie') size
> 3
Accessing the 2nd element using at: anIndex
#('Calvin' 'hates' 'Suzie') at: 2
> 'hates'
Remember collection index starts at 1
#('Calvin' 'hates' 'Suzie') at: 55
> SubscriptOutOfBounds Error
Use the message at: anIndex put: anObject
Modifying the second element of the receiver
#('Calvin' 'hates' 'Suzie') at: 2 put: 'loves'
> #('Calvin' 'loves' 'Suzie')
Literal arrays contain objects that have a textual (literal) representation: numbers, strings, nil, symbols
#(45 'milou' 1300 true #tintin)
> #(45 'milou' 1300 true #tintin)
They are instances of the class Array
#(45 38 1300 8) class
> Array
Literal arrays are equivalent to a dynamic version
A literal array
#(45 38 'milou' 8)
> #(45 38 'milou' 8)
An array
Array with: 45 with: 38 with: 'milou' with: 8
> #(45 38 'milou' 8)
add:
, remove:
| ordCol |
ordCol := OrderedCollection new.
ordCol add: 'Reef'; add: 'Pharo'; addFirst: 'Pharo'.
ordCol
> an OrderedCollection('Pharo' 'Reef' 'Pharo')
ordCol add: 'Seaside'.
ordCol
> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Seaside')
#('Pharo' 'Reef' 'Pharo' 'Pharo') asOrderedCollection
> an OrderedCollection('Pharo' 'Reef' 'Pharo' 'Pharo')
add:
, remove:
#('Pharo' 'Reef' 'Pharo' 'Pharo') asSet
> a Set('Pharo' 'Reef')
Set with: (Set with: 1) with: (Set with: 2)
Collections can be converted simply to other collections
asOrderedCollection
asSet
asArray
at:
, at:ifAbsent:
at:put:
, at:ifAbsentPut:
do:
, keysDo:
, keysAndValuesDo:
| days |
days := Dictionary new.
days
at: #January put: 31;
at: #February put: 28;
at: #March put: 31.
| days |
days := Dictionary new.
days
at: #January put: 31;
at: #February put: 28;
at: #March put: 31.
is equivalent to
{ #January -> 31.
#February -> 28.
#March -> 31} asDictionary
(#January -> 31) key
> #January
(#January -> 31) value
> 31
| days |
days := Dictionary new.
days
at: #January put: 31;
at: #February put: 28;
at: #March put: 31.
days at: #January
> 31
days at: #NoMonth
> KeyNotFound Error
days at: #NoMonth ifAbsent: [0]
> 0
days do: [ :each | Transcript show: each ;cr ]
prints
31
28
31
Why? Because
Dictionary >> do: aBlock
^self valuesDo: aBlock
days keysAndValuesDo:
[ :k :v | Transcript show: k asString, ' has ', v printString, ' days' ; cr ]
shows:
January has 31 days
February has 28 days
March has 31 days
/