4. Arrays


"Arrays are also objects in Smalltalk.
The class Array is a subclass (though not an immediate subclass) of
Collection.  All collections share enumeration and mutation abilities,
though some kinds of collection impose limits.  For example, Arrays are
of a fixed size, so it is not possible to add more elements to an Array
than it's current size - with other kinds of collection, any number of
elements can be added (until you hit a system limit, like running out of

"Arrays have a literal form, unlike other kinds of collection."
"First we assign a litteral Array containing four Strings to the
variable myArray."
myArray := #('this' 'that' 'the' 'other').
"Now we create a nested Array in a similar way.  We end up with an Array
with three elements, the first two being Strings and the third being a
distinct instance of class Array in turn containing two Strings."
myArray := #('this' 'that' #('the' 'other')).

"We can also create arrays by message passing.  So, to repeat the last
last example:"
|outer nested|
nested := Array with: 'the' with: 'other'.
outer := Array with: 'this' with: 'that' with: nested.

Specifying a List In Your Program

|a lines|
a := #('quick' 'brown' 'fox').
"The following send >>subStrings to a String.  The result of this is an
OrderedCollection with 5 elements, each element being a String.  To get
an Array, we send >>asArray to the OrderedCollection."
a := 'Why are you teasing me?' subStrings asArray.

"In the next example I'm forcing the cr character into the string to
make the code work either from a code file, or in an image workspace.
The cr in a literal String *should* be respected, but in GNU Smalltalk
2.1.8 it does not seem to be in an image worksheet."
lines := (
    ('The boy stood on the burning deck.%1' bindWith: Character cr),
     'It was as hot as glass.' subStrings: Character cr) asArray.

"Here we create a large array by reading the contents of a file.  The
contents of the file is an instance of class String in this case.
>>contents is crude, but works fine in this case ... if you have
a file called 'mydatafile' in the current directory that contains
printable characters."
bigArray := ((File name: 'mydatafile') contents) asArray.

"Now we concatenate some Strings and convert them to Arrays"
|name banner|
name := 'Gandalf'.
banner := ('Speak, ', name, ', and enter!') asArray.
banner := ('Speak, %1, and enter!' bindWithArguments: (Array with:
name)) asArray.

Printing a List with Commas

Changing Array Size

Doing Something with Every Element in a List

Iterating Over an Array by Reference

Extracting Unique Elements from a List

Finding Elements in One Array but Not Another

Computing Union, Intersection, or Difference of Unique Lists

Appending One Array to Another

Reversing an Array

Processing Multiple Elements of an Array

Finding the First List Element That Passes a Test

Finding All Elements in an Array Matching Certain Criteria

Sorting an Array Numerically

Sorting a List by Computable Field

Implementing a Circular List

Randomizing an Array

Program: words

Program: permute