Arrays in Smalltalk

The Array class in Smalltalk is a subclass of ArrayedCollection (you will find it in the browser by looking at the Category Collections-Arrayed). Objects that are instances of the Array class are fixed size objects that can be indexed, starting from 1. To create an array object:

a _ Array new: 100

creates an array object with 100 elements.

An array can hold any type of a data and each entry in the array can be a different object. To place an object in a specific location in the array, do

a at: i put: o

puts object, o in position i in the array object a, i.e. a[i] = o.

Objects stored in an array can be accessed as follows:

a at: i

returns the object at index i, i.e. a[i].

For more information, browse the class hierarchy in Squeak and look at myriads of other methods available.

Ordered Collections in Smalltalk

While all objects of type Array are fixed size arrays, instances of class OrderedCollection can be of varying size. To create an OrderedCollection object do:

a _ OrderedCollection new.

creates a new ordered collection. To store something in it, you can simply add:

a add: o

adds object, o to the collection a. Elements in an ordered collection can be accessed just like array elements. I.e.,

a at: i

returns the ithe element in a.

OrderedCollection is a subclass of SequenceableCollection (in case you want to browse it).