ArrayList

Inherits from: Object

A growable, ordered collection backed by a Go slice.

ArrayList provides amortized O(1) append via add:, O(1) indexed access, and efficient select:, reject:, collect: operations — all without the O(n^2) copying penalty of building Arrays incrementally with copyWith: or concatenation.

Use ArrayList when building up a collection element by element, then call asArray if you need a fixed-size Array at the end.

Test
ArrayList new size >>> 0
ArrayList new isEmpty >>> true
Example
"Build a list incrementally"
| list |
list := ArrayList new.
list add: 1.
list add: 2.
list add: 3.
list size

Class Methods

primitives

class new

Create a new empty ArrayList with default capacity.

Test
ArrayList new size >>> 0
ArrayList new isEmpty >>> true
class new:

Create a new empty ArrayList with the given initial capacity. The capacity is a hint for how many elements will be added.

Test
(ArrayList new: 100) size >>> 0
(ArrayList new: 100) capacity >= 100 >>> true
class primNew
class primNew:
class primWithAll:
class withAll:

Create a new ArrayList containing all elements of the given Array.

Test
(ArrayList withAll: #(1 2 3)) size >>> 3
(ArrayList withAll: #(10 20)) first >>> 10

Instance Methods

primitives

add:

Append an element to the end. Amortized O(1). Returns self.

Test
(ArrayList new add: 42) size >>> 1
addAll:

Append all elements from a collection (Array or ArrayList). Returns self.

Test
(ArrayList withAll: #(1 2 3)) size >>> 3
asArray

Return a new fixed-size Array containing all elements.

Test
(ArrayList withAll: #(1 2 3)) asArray >>> #(1 2 3 )
ArrayList new asArray >>> #()
at:

Return the element at a 0-based index.

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

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

Test
(ArrayList withAll: #(10 20 30)) at: 1 put: 99 >>> 99
capacity

Return the current backing capacity.

Test
(ArrayList new: 50) capacity >= 50 >>> true
clear

Remove all elements, keeping the backing capacity. Returns self.

Test
(ArrayList withAll: #(1 2 3)) clear isEmpty >>> true
collect:

Return a new ArrayList with block applied to each element.

Test
((ArrayList withAll: #(1 2 3)) collect: [:x | x * 2]) asArray >>> #(2 4 6 )
copy

Return a new ArrayList with the same elements.

detect:

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

Test
(ArrayList withAll: #(1 2 3 4 5)) detect: [:x | x > 3] >>> 4
(ArrayList withAll: #(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
(ArrayList withAll: #(1 2 3)) detect: [:x | x > 10] ifNone: [0] >>> 0
do:

Evaluate block once for each element.

first

Return the first element, or nil if empty.

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

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

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

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

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

Accumulate a value by applying block to each element in turn.

Test
(ArrayList withAll: #(1 2 3 4)) inject: 0 into: [:sum :x | sum + x] >>> 10
isEmpty

Return true if the list has no elements.

Test
ArrayList new isEmpty >>> true
(ArrayList withAll: #(1)) isEmpty >>> false
last

Return the last element, or nil if empty.

Test
(ArrayList withAll: #(10 20 30)) last >>> 30
notEmpty

Return true if the list has at least one element.

Test
(ArrayList withAll: #(1)) notEmpty >>> true
ArrayList new notEmpty >>> false
primAdd:
primAddAll:
primAsArray
primAt:
primAt:put:
primCapacity
primClear
primCollect:
primCopy
primDetect:
primDetect:ifNone:
primDo:
primFirst
primIncludes:
primIndexOf:
primInject:into:
primIsEmpty
primLast
primNotEmpty
primPrintString
primReject:
primRemoveAt:
primRemoveLast
primSelect:
primSize
primSort:
printString

Return a string representation.

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

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

Test
((ArrayList withAll: #(1 2 3 4 5)) reject: [:x | x > 3]) asArray >>> #(1 2 3 )
removeAt:

Remove and return the element at index, shifting remaining elements.

Test
(ArrayList withAll: #(10 20 30)) removeAt: 1 >>> 20
removeLast

Remove and return the last element. O(1).

Test
(ArrayList withAll: #(1 2 3)) removeLast >>> 3
select:

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

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

Return the number of elements.

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

Sort the list 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 self.

uncategorized

,

Return a new ArrayList that is the concatenation of self and other.