ArrayList - PHP ArrayList like in Java ArrayList
ArrayList can validate:
- Scalar types:
string
,integer
, etc.:
(new \ArrayList\ArrayList('string')); // => Construct ArrayList with empty values type 'string'
(new \ArrayList\ArrayList('string', ['1', '2', '3'])); // => to construct object
(new \ArrayList\ArrayList('string', [1, 2, 3])); // => to be thow exception
(new \ArrayList\ArrayList('string', (new \ArrrayList\ArrayList('string', ['foo', 'bar'])))); // => Construct ArrayList with values from other ArrayList
ArrayList have methods with overloaded:
- method add($element). Appends the specified element to the end of this list.
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->add(4); // => wrong type. Throw expection
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->add('4'); // => append element to end of list
- method add(int $index, $element) Inserts the specified element at the specified position in this list.
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->add(2,'4'); // => return true
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->add(2, 4); // throw exepction
- method addAll(ArrayList\Collection $element) Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->addAll((new \ArrayList\ArrayList('string', ['1', '2', '3'])); // => return true
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->addAll((new \ArrayList\ArrayList('integer', [1, 2, 3])); // => throw exception
- method addAll(int $index, ArrayList\Collection $element) Inserts all of the elements in the specified collection into this list, starting at the specified position.
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->addAll(2, (new \ArrayList\ArrayList('string', ['1', '2', '3'])); // => return true
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->addAll(2, (new \ArrayList\ArrayList('integer', [1, 2, 3])); // => throw exception
- method isEmpty() Returns true if this list contains no elements.
(new \ArrayList\ArrayList('string'))->isEmtpy(); // return true
(new \ArrayList\ArrayList('string', ['1', '2', '3']))->isEmtpy(); //to be false
- method get(int $index) Returns the element at the specified position in this list.
(new \ArrayList\ArrayList('string', ['string1']))->get(1); // throw OutOfBoundsException
(new \ArrayList\ArrayList('string', ['string1']))->get(0); // return 'string1'
- method set(int $index) Replaces the element at the specified position in this list with the specified element.
(new \ArrayList\ArrayList('string', ['string1']))->set(1, 'string2'); // throw OutOfBoundsException
(new \ArrayList\ArrayList('string', ['string1']))->set(0, 'string2'); // set new value
(new \ArrayList\ArrayList('string', ['string1']))->set(0, 2); // throw exception
- method size() Returns the number of elements in this list.
(new \ArrayList\ArrayList('string', ['string1']))->size(); // return size ArrayList
- method contains($element) Returns true if this list contains the specified element.
(new \ArrayList\ArrayList('string', ['string1']))->constains('string1'); // return true
- method containsAll(ArrayList\Collection $element) Returns true if this collection contains all of the elements in the specified collection.
(new \ArrayList\ArrayList('string', ['string1']))->constains((new \ArrayList\ArrayList('string', ['string1'])); // return true
- method indexOf($element) Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
(new \ArrayList\ArrayList('string',['string1','strign2']))->indexOf('string1'); // return 0
- method lastIndexOf($element) Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
(new \ArrayList\ArrayList('string', ['string1', 'strign1']))->lastIndexOf('string1'); // return 1
- method toArray() Returns an array containing all of the elements in this list in proper sequence (from first to last element).
(new \ArrayList\ArrayList('string', ['string1', 'strign1']))->toArray(); // return ['string1','strign1']
- method clear() Removes all of the elements from this list.
(new \ArrayList\ArrayList('string', ['string1', 'strign1']))->clear(); // clear values
- method remove($index) Removes the element at the specified position in this list.
(new \ArrayList\ArrayList('string',['string1','strign1']))->remove(0); // return 'string1'
- method remove($element) Removes the first occurrence of the specified element from this list, if it is present.
(new \ArrayList\ArrayList('string', ['string1', 'strign1']))->remove('string1'); // return 'string1'
- method removeAll(ArrayList\Collection $collection) Removes from this list all of its elements that are contained in the specified collection.
(new \ArrayList\ArrayList('string', ['string1', 'strign1']))->removeAll((new ArrayList\ArrayList('string', ['string1'])); // return true
- method removeRange(int $from, int to) Removes from this list all of its elements that are contained in the specified collection.
(new \ArrayList\ArrayList('string', ['string1', 'strign1', 'strign1']))->removeRange(0, 1); // return true