Array: Difference between revisions

Content added Content deleted
Line 209: Line 209:


===[[Smalltalk]]===
===[[Smalltalk]]===
Smalltalk provides a rich set of collection classes in its base library, which is also standard (ANSI) among the different implementations. The most used are:
<lang smalltalk></lang>
Array - fixed size, indexed by integer index, any kind of element
ByteArray - an array of bytes, like Array, but only byte valued integers as elements
String - an array of characters, like Array, but only characters as elements
OrderedCollection - variable size, indexed by integer index, any kind of element
Dictionary - variable size, indexed by arbitrary object as index, any kind of element

Array, ByteArray, String and all others provide a common protocol which they inherit from their common superclass, Collection. ByteArray and String can be considered as Arrays which use a more compact storage representation internally (and may provide additional protocol eg. for case conversion or bit-array operations).

Array
<lang smalltalk>"an array literal (i.e. constant); this is immutable"
a1 := #(10 20 30 40).

"another array literal (i.e. constant); also immutable; notice the 5th element being another array"
a2 := #(1.0 20.0 'thirty' true #(5 5 5)).
"a new created array; all 10 elements are nil"
a4 := Array new:10. "all 10 elements are nil"

"an new created array with computed elements"
a3 := { 10 . 20 . (a1 at:3) . 40 squared }.</lang>

accessing:
<lang smalltalk>"access by index"
a1 at:2. -> 20
a1 at:index put:100. -> error; immutable
a2 at:4 -> 1600
a2 at:4 put:99.999.

"asking for the size"
a1 size -> 4

"concatenating"
a1,a2 -> #(10 20 30 40 1.0 20.0 'thirty' true (5 5 5)

"enumerating"
a1 do:[:e | e printCR ]
a1 select:[:e | e > 20]. -> #(30 40)
a1 collect:[e | e squared]. -> #(100 400 900 1600)
a1 collect:#squared -> ditto
a1 count:[:e | e >= 20]. -> 3
a1 occurrencesOf:10 -> 10
.. there are many many more such functions..

"searching"
a1 indexOf:40. -> 4
a1 lastIndexOf:40. -> 4
a1 findFirst:[:e | e > 10] -> 2
a1 findLast:[:e | e > 10] -> 4
a1 detect:[:e | e > 10] -> 20</lang>

ByteArray
<lang smalltalk>"a Byterray literal (i.e. constant); this is immutable"
b1 := #[10 20 30 40].
all of the above also works for ByteArrays</lang>

String
<lang smalltalk>"a String literal (i.e. constant); this is immutable"
s1 := 'hello'.
s2 := 'здрависване'.
"all of the above also works for Strings; i.e."
s1 occurencesOf:$l -> 2
s1,' ',s2 -> 'hello здрависване'.</lang>

OrderedCollection
<lang smalltalk>"these are like arrays, but they can also grow or shrink"
o1 := #(10 20 30 40) asOrderedCollection.
o1 add:'fifty'.
o1 addAll: #(60 70.0).
o1 addAll: a1.
o1 addAll: s1.
o1 -> OrderedCollection(10 20 30 40 'fifty' 60 70.0 10 20 30 40 $h $e $l $l $o).
o1 removeLast:4.
o1 removeFirst:4.
o1 -> OrderedCollection('fifty' 60 70.0 10 20 30 40 $h).
o1 addFirst:1.
o1 add:22 beforeIndex:3.

"of course, all of the above also work for OrderedCollections"</lang>

Dictionary
<lang smalltalk>"these can take any object as key"
d1 := Dictionary new.
d1 at:1 put:'one'. "an integer works as key"
d1 at:'one' put:1. "an string as key"
d1 at:a1 put:42. "an array as key"
d1 at:'one'. -> 1 "access by key"
d1 at:#(10 20 30 40). -> 42 "any object as key, again
d1 printCR -> Dictionary(1->one one->1 #(10 20 30 40)->42)
d1 removeKey:'one'
d1 includesKey:'two' -> false. "is key present"
d1 includes:2 -> false. "is value present; attention: not O(1)"

Overall there are hundreds of methods implemented in Collection, from which all of the above inherit.


[[Category:Data Structures]]
[[Category:Data Structures]]