[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Creating an array

From: Stephen Woolerton
Subject: Re: [Help-smalltalk] Creating an array
Date: Tue, 13 Aug 2013 09:33:21 +1200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8

There is a shorthand for describing the messages you send to objects. You just run the message names together. So we would say that our array accepts both the at: and at:put: messages.[/quote]


x := Array new: 20
x at: 1 put: 99

What would be the shortcut syntax for this array assignment? Or am I misunderstanding what I quoted above?? TIA...


I think the quote you've referenced is just describing how messages are communicated. For example: shorthand for other messages that Array accepts are 'printOn: aStream' and 'replaceFrom: start to: stop with: byteArray startingAt: replaceStart'.

What I think you want to know is how to create an array with a shortcut syntax...

If at the time the array is created, the elements of the array are already known, then there is a shortcut syntax:
x := { 99. } will create an array of size 1 as per your example above.

x := { 'abc'. 1. 2. 'abcd'. } will create an array of size 3 and is equivalent to x := Array with: 'abc' with: 1 with: 2 with: 'abcd'. (you can only create arrays with a max of five items using this syntax)

Arrays are of fixed size. If you want a variable size collection, use OrderedCollection

y := x asOrderedCollection.
y := y add: 5; yourself.
y := { 'abc'. 1. 2. 'abcd'. } asOrderedCollection.
y := (OrderedCollection new) add: 1; add: 2; yourself.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]