Overview
ListCollection provides several methods for adding elements. All methods ensure that sequential integer keys are maintained.push()
Add one or more elements to the end of the collection.Signature
One or more values to append to the collection.
Examples
prepend()
Add an element to the beginning of the collection. The second parameter (key) is ignored to maintain list structure.Signature
The value to prepend to the collection.
Ignored parameter (for compatibility with parent class).
Examples
offsetSet()
Set a value at a specific index using array access syntax. This method intelligently handles different key types.Signature
The index to set. Use
null or omit to append.The value to set.
Behavior
nullkey: Appends to the end- Valid integer (0 ≤ key ≤ count): Replaces value at that index
- Invalid key (negative, out of range, or string): Appends to the end
Examples
Append with null key
Replace at valid index
Append when index equals count
Append with string key
Append with out-of-range integer
Append with negative integer
add()
Append a value to the collection and maintain sequential keys.Signature
The value to append.
Examples
Key Points
- All adding methods preserve sequential integer keys (0, 1, 2, …)
push()andadd()append to the endprepend()adds to the beginningoffsetSet()intelligently handles various key types, defaulting to append for invalid keys- Invalid or associative keys are ignored—values are always added with sequential indices