Array
An ordered, fixed-size collection of elements accessed by 0-based index.
Arrays are created with Array new: size (all elements nil) or with the
literal syntax #(1 2 3). Elements can be any object. Use collect:,
select:, reject:, and inject:into: for functional-style transformations.
(Array new: 3) size >>> 3
#(10 20 30) first >>> 10
#(10 20 30) last >>> 30
"Filter and transform a collection"
(#(1 2 3 4 5 6 7 8 9 10) select: [:n | n odd]) collect: [:n | n * n]
Class Methods
primitives
Instance Methods
primitives
uncategorized
Return a new array that is the concatenation of the receiver and other.
#(1 2), #(3 4) >>> #(1 2 3 4 )
#(), #(1 2) >>> #(1 2 )
Return the element at a 0-based index.
#(10 20 30) at: 0 >>> 10
#(10 20 30) at: 2 >>> 30
Set the element at a 0-based index. Returns the value stored.
a := Array new: 3.
a at: 1 put: 42.
a at: 1 >>> 42
Return a new array with block applied to each element.
#(1 2 3) collect: [:x | x * 2] >>> #(2 4 6 )
#(1 4 9) collect: [:x | x + 1] >>> #(2 5 10 )
"Convert numbers to strings"
#(1 2 3) collect: [:n | n printString]
Return a copy of this array.
a := #(1 2 3).
b := a copy.
b at: 0 put: 99.
a at: 0 >>> 1
Return a new array containing elements from start index to the end. 0-based indexing.
#(10 20 30 40 50) copyFrom: 2 >>> #(30 40 50 )
#(10 20 30) copyFrom: 0 >>> #(10 20 30 )
#(10 20 30) copyFrom: 3 >>> #()
Return a new array with elements from start (inclusive) to end (exclusive). Indices are 0-based.
#(10 20 30 40 50) copyFrom: 1 to: 4 >>> #(20 30 40 )
#(10 20 30) copyFrom: 0 to: 2 >>> #(10 20 )
Return a new array with element appended at the end.
#(1 2) copyWith: 3 >>> #(1 2 3 )
#() copyWith: 42 >>> #(42 )
Return the first element for which block returns true, or nil if none.
#(1 2 3 4 5) detect: [:x | x > 3] >>> 4
#(1 2 3) detect: [:x | x > 10] >>> nil
Return the first element for which block returns true. If none found, evaluate noneBlock and return its result.
#(1 2 3) detect: [:x | x > 10] ifNone: [0] >>> 0
#(1 2 3) detect: [:x | x > 1] ifNone: [0] >>> 2
Evaluate block once for each element in the array.
sum := 0.
#(1 2 3) do: [:each | sum := sum + each].
sum >>> 6
Return the first element of the array.
#(10 20 30) first >>> 10
Return true if the array contains an element equal to anObject.
#(1 2 3) includes: 2 >>> true
#(1 2 3) includes: 5 >>> false
Return the 0-based index of anObject, or -1 if not found.
#(10 20 30) indexOf: 20 >>> 1
#(10 20 30) indexOf: 99 >>> -1
Accumulate a value by applying block to each element in turn. Block receives the accumulator and the current element.
#(1 2 3 4) inject: 0 into: [:sum :x | sum + x] >>> 10
#(1 2 3 4) inject: 1 into: [:prod :x | prod * x] >>> 24
"Find the longest string"
#('cat' 'elephant' 'dog') inject: '' into: [:longest :s |
s size > longest size ifTrue: [s] ifFalse: [longest]
]
Return true if the array has no elements.
(Array new: 0) isEmpty >>> true
#(1) isEmpty >>> false
Join array elements into a string with a separator. Alias for joinWith: — uses the raw string value of each element.
#('a' 'b' 'c') join: ', ' >>> 'a, b, c'
#() join: ', ' >>> ''
#('x') join: ', ' >>> 'x'
Join array elements into a string with a separator. Elements should be strings; non-string elements are treated as empty.
#('a' 'b' 'c') joinWith: ', ' >>> 'a, b, c'
#() joinWith: ', ' >>> ''
#('x') joinWith: ', ' >>> 'x'
Return the last element of the array.
#(10 20 30) last >>> 30
#(42) last >>> 42
Return true if the array has at least one element.
#(1 2) notEmpty >>> true
(Array new: 0) notEmpty >>> false
Return a string representation of the array.
#(1 2 3) printString >>> '#(1 2 3 )'
#() printString >>> '#()'
Return a new array excluding elements for which block returns true.
#(1 2 3 4 5) reject: [:x | x > 3] >>> #(1 2 3 )
#(1 2 3 4) reject: [:x | x even] >>> #(1 3 )
Return a new array containing only elements for which block returns true.
#(1 2 3 4 5) select: [:x | x > 3] >>> #(4 5 )
#(1 2 3 4) select: [:x | x even] >>> #(2 4 )
Return the number of elements in the array.
#(1 2 3) size >>> 3
(Array new: 0) size >>> 0
Sort the array in-place using default < comparison. Returns the receiver.
a := #(3 1 2) copy.
a sort.
a >>> #(1 2 3 )
Sort the array in-place using a comparison block. The block receives two elements and should return a negative number (a < b), zero (a = b), or positive number (a > b). Returns the receiver.
a := #(3 1 2) copy.
a sort: [:a :b | a - b].
a >>> #(1 2 3 )
Return a new sorted array using default < comparison (non-destructive).
a := #(3 1 2).
a sorted >>> #(1 2 3 )
a >>> #(3 1 2 )
Return a new sorted array using a comparison block (non-destructive). The block receives two elements and should return a negative number (a < b), zero (a = b), or positive number (a > b).
a := #(3 1 2).
a sorted: [:a :b | a - b] >>> #(1 2 3 )
a >>> #(3 1 2 )