ArrayList
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.
ArrayList new size >>> 0
ArrayList new isEmpty >>> true
"Build a list incrementally"
| list |
list := ArrayList new.
list add: 1.
list add: 2.
list add: 3.
list size
Class Methods
primitives
Create a new empty ArrayList with default capacity.
ArrayList new size >>> 0
ArrayList new isEmpty >>> true
Create a new empty ArrayList with the given initial capacity. The capacity is a hint for how many elements will be added.
(ArrayList new: 100) size >>> 0
(ArrayList new: 100) capacity >= 100 >>> true
Create a new ArrayList containing all elements of the given Array.
(ArrayList withAll: #(1 2 3)) size >>> 3
(ArrayList withAll: #(10 20)) first >>> 10
Instance Methods
primitives
Append an element to the end. Amortized O(1). Returns self.
(ArrayList new add: 42) size >>> 1
Append all elements from a collection (Array or ArrayList). Returns self.
(ArrayList withAll: #(1 2 3)) size >>> 3
Return a new fixed-size Array containing all elements.
(ArrayList withAll: #(1 2 3)) asArray >>> #(1 2 3 )
ArrayList new asArray >>> #()
Return the element at a 0-based index.
(ArrayList withAll: #(10 20 30)) at: 0 >>> 10
(ArrayList withAll: #(10 20 30)) at: 2 >>> 30
Set the element at a 0-based index. Returns the value stored.
(ArrayList withAll: #(10 20 30)) at: 1 put: 99 >>> 99
Return the current backing capacity.
(ArrayList new: 50) capacity >= 50 >>> true
Remove all elements, keeping the backing capacity. Returns self.
(ArrayList withAll: #(1 2 3)) clear isEmpty >>> true
Return a new ArrayList with block applied to each element.
((ArrayList withAll: #(1 2 3)) collect: [:x | x * 2]) asArray >>> #(2 4 6 )
Return a new ArrayList with the same elements.
Return the first element for which block returns true, or nil if none.
(ArrayList withAll: #(1 2 3 4 5)) detect: [:x | x > 3] >>> 4
(ArrayList withAll: #(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.
(ArrayList withAll: #(1 2 3)) detect: [:x | x > 10] ifNone: [0] >>> 0
Evaluate block once for each element.
Return the first element, or nil if empty.
(ArrayList withAll: #(10 20 30)) first >>> 10
Return true if the list contains an element equal to anObject.
(ArrayList withAll: #(1 2 3)) includes: 2 >>> true
(ArrayList withAll: #(1 2 3)) includes: 5 >>> false
Return the 0-based index of anObject, or -1 if not found.
(ArrayList withAll: #(10 20 30)) indexOf: 20 >>> 1
(ArrayList withAll: #(10 20 30)) indexOf: 99 >>> -1
Accumulate a value by applying block to each element in turn.
(ArrayList withAll: #(1 2 3 4)) inject: 0 into: [:sum :x | sum + x] >>> 10
Return true if the list has no elements.
ArrayList new isEmpty >>> true
(ArrayList withAll: #(1)) isEmpty >>> false
Return the last element, or nil if empty.
(ArrayList withAll: #(10 20 30)) last >>> 30
Return true if the list has at least one element.
(ArrayList withAll: #(1)) notEmpty >>> true
ArrayList new notEmpty >>> false
Return a string representation.
(ArrayList withAll: #(1 2 3)) printString >>> 'ArrayList(1 2 3)'
ArrayList new printString >>> 'ArrayList()'
Return a new ArrayList excluding elements for which block returns true.
((ArrayList withAll: #(1 2 3 4 5)) reject: [:x | x > 3]) asArray >>> #(1 2 3 )
Remove and return the element at index, shifting remaining elements.
(ArrayList withAll: #(10 20 30)) removeAt: 1 >>> 20
Remove and return the last element. O(1).
(ArrayList withAll: #(1 2 3)) removeLast >>> 3
Return a new ArrayList containing only elements for which block returns true.
((ArrayList withAll: #(1 2 3 4 5)) select: [:x | x > 3]) asArray >>> #(4 5 )
Return the number of elements.
(ArrayList withAll: #(1 2 3)) size >>> 3
ArrayList new size >>> 0
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.