Contents

Class Array


Inheritance:

Object  »
  Collection  »
    SequenceableCollection  »
      ArrayedCollection  »
        Array

An Array is a collection of any objects accessed through a fixed range of Integer indices (representing the positions of the elements within the Array). Class Array inherits most of its protocol from its superclasses.

Array indices start with 1 and run up to the array size.

Array is a variable subclass. Instances are created with the message new: integer, where integer is the array size, that is the number of its elements. An array is always completely initialized with nil values. Unlike most other collections, arrays are created at a fixed size and cannot grow.

A writing form for constant arrays is available. It looks like:

#(1 2 'Hello' $c #close true false nil #('a' 'b'))

For Squeak versions up to 3.6, the classes of the elements in this array are obtained with:

#(1 2 'Hello' $c #close true false nil #('a' 'b'))
   collect: [:elem | elem class]
#(SmallInteger SmallInteger String Character
  Symbol Symbol Symbol Symbol Array)

Note that true, false and nil are interpreted as Symbols. This is the interpretation of the early Smalltalk systems.

In Squeak 3.7 as well as in all newer Smalltalks, we have:

#(1 2 'Hello' $c #close true false nil #('a' 'b'))
   collect: [:elem | elem class]
#(SmallInteger SmallInteger String Character
  Symbol True False UndefinedObject Array)

In Squeak 3.8, things were changed again and we have:

#(1 2 'Hello' $c #close true false nil #('a' 'b'))
   collect: [:elem | elem class]
#(SmallInteger SmallInteger ByteString Character
  ByteSymbol True False UndefinedObject Array)

Classes ByteString and ByteSymbol were introduced into Squeak 3.8 as part of the support for multilingual writing. They are not available in earlier versions.

The methods to access array elements are at: and at:put:, these methods are inherited from Object. The assignment method at:put: answers the value that it stored. It is widely believed that this is a convention that is both arbitrary and inconvenient, but in fact it is this convention that allows us to write a nested assignment in the following way:

  | array1 array2 val | 
 array1 := Array new: 10.
 array2 := Array new: 10.
 val := array1 at: 2 put: (array2 at: 2 put: 10 sqrt * Float pi).

Contents