Array

Inherits from: Object

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.

Test
(Array new: 3) size >>> 3
#(10 20 30) first >>> 10
#(10 20 30) last >>> 30
Example
"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

class new
class new:
class new:withAll:
class with:
class with:with:

Instance Methods

primitives

primAt:
primAt:put:
primJoinWith:
primSize
primSort:
primSortDefault
primSorted:
primSortedDefault

uncategorized

,

Return a new array that is the concatenation of the receiver and other.

Test
#(1 2), #(3 4) >>> #(1 2 3 4 )
#(), #(1 2) >>> #(1 2 )
at:

Return the element at a 0-based index.

Test
#(10 20 30) at: 0 >>> 10
#(10 20 30) at: 2 >>> 30
at:put:

Set the element at a 0-based index. Returns the value stored.

Test
a := Array new: 3.
a at: 1 put: 42.
a at: 1 >>> 42
collect:

Return a new array with block applied to each element.

Test
#(1 2 3) collect: [:x | x * 2] >>> #(2 4 6 )
#(1 4 9) collect: [:x | x + 1] >>> #(2 5 10 )
Example
"Convert numbers to strings"
#(1 2 3) collect: [:n | n printString]
copy

Return a copy of this array.

Test
a := #(1 2 3).
b := a copy.
b at: 0 put: 99.
a at: 0 >>> 1
copyFrom:

Return a new array containing elements from start index to the end. 0-based indexing.

Test
#(10 20 30 40 50) copyFrom: 2 >>> #(30 40 50 )
#(10 20 30) copyFrom: 0 >>> #(10 20 30 )
#(10 20 30) copyFrom: 3 >>> #()
copyFrom:to:

Return a new array with elements from start (inclusive) to end (exclusive). Indices are 0-based.

Test
#(10 20 30 40 50) copyFrom: 1 to: 4 >>> #(20 30 40 )
#(10 20 30) copyFrom: 0 to: 2 >>> #(10 20 )
copyWith:

Return a new array with element appended at the end.

Test
#(1 2) copyWith: 3 >>> #(1 2 3 )
#() copyWith: 42 >>> #(42 )
detect:

Return the first element for which block returns true, or nil if none.

Test
#(1 2 3 4 5) detect: [:x | x > 3] >>> 4
#(1 2 3) detect: [:x | x > 10] >>> nil
detect:ifNone:

Return the first element for which block returns true. If none found, evaluate noneBlock and return its result.

Test
#(1 2 3) detect: [:x | x > 10] ifNone: [0] >>> 0
#(1 2 3) detect: [:x | x > 1] ifNone: [0] >>> 2
do:

Evaluate block once for each element in the array.

Test
sum := 0.
#(1 2 3) do: [:each | sum := sum + each].
sum >>> 6
first

Return the first element of the array.

Test
#(10 20 30) first >>> 10
includes:

Return true if the array contains an element equal to anObject.

Test
#(1 2 3) includes: 2 >>> true
#(1 2 3) includes: 5 >>> false
indexOf:

Return the 0-based index of anObject, or -1 if not found.

Test
#(10 20 30) indexOf: 20 >>> 1
#(10 20 30) indexOf: 99 >>> -1
inject:into:

Accumulate a value by applying block to each element in turn. Block receives the accumulator and the current element.

Test
#(1 2 3 4) inject: 0 into: [:sum :x | sum + x] >>> 10
#(1 2 3 4) inject: 1 into: [:prod :x | prod * x] >>> 24
Example
"Find the longest string"
#('cat' 'elephant' 'dog') inject: '' into: [:longest :s |
    s size > longest size ifTrue: [s] ifFalse: [longest]
]
isEmpty

Return true if the array has no elements.

Test
(Array new: 0) isEmpty >>> true
#(1) isEmpty >>> false
join:

Join array elements into a string with a separator. Alias for joinWith: — uses the raw string value of each element.

Test
#('a' 'b' 'c') join: ', ' >>> 'a, b, c'
#() join: ', ' >>> ''
#('x') join: ', ' >>> 'x'
joinWith:

Join array elements into a string with a separator. Elements should be strings; non-string elements are treated as empty.

Test
#('a' 'b' 'c') joinWith: ', ' >>> 'a, b, c'
#() joinWith: ', ' >>> ''
#('x') joinWith: ', ' >>> 'x'
last

Return the last element of the array.

Test
#(10 20 30) last >>> 30
#(42) last >>> 42
notEmpty

Return true if the array has at least one element.

Test
#(1 2) notEmpty >>> true
(Array new: 0) notEmpty >>> false
printString

Return a string representation of the array.

Test
#(1 2 3) printString >>> '#(1 2 3 )'
#() printString >>> '#()'
reject:

Return a new array excluding elements for which block returns true.

Test
#(1 2 3 4 5) reject: [:x | x > 3] >>> #(1 2 3 )
#(1 2 3 4) reject: [:x | x even] >>> #(1 3 )
select:

Return a new array containing only elements for which block returns true.

Test
#(1 2 3 4 5) select: [:x | x > 3] >>> #(4 5 )
#(1 2 3 4) select: [:x | x even] >>> #(2 4 )
size

Return the number of elements in the array.

Test
#(1 2 3) size >>> 3
(Array new: 0) size >>> 0
sort

Sort the array in-place using default < comparison. Returns the receiver.

Test
a := #(3 1 2) copy.
a sort.
a >>> #(1 2 3 )
sort:

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.

Test
a := #(3 1 2) copy.
a sort: [:a :b | a - b].
a >>> #(1 2 3 )
sorted

Return a new sorted array using default < comparison (non-destructive).

Test
a := #(3 1 2).
a sorted >>> #(1 2 3 )
a >>> #(3 1 2 )
sorted:

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).

Test
a := #(3 1 2).
a sorted: [:a :b | a - b] >>> #(1 2 3 )
a >>> #(3 1 2 )