CList implements an integer-indexed collection class.
You can access, append, insert, remove an item by using CList::itemAt()
,
CList::add()
, CList::insertAt()
, CList::remove()
, and CList::removeAt()
. To get the
number of the items in the list, use CList::getCount()
. CList can also be used
like a regular array as follows,
$list[]=$item; // append at the end
$list[$index]=$item; // $index must be between 0 and $list->Count
unset($list[$index]); // remove the item at $index
if(isset($list[$index])) // if the list has an item at $index
foreach($list as $index=>$item) // traverse each item in the list
$n=count($list); // returns the number of items in the list
To extend CList by doing additional operations with each addition or removal
operation (e.g. performing type check), override CList::insertAt()
, and CList::removeAt()
.
Methods summary
public
|
#
__construct( array $data = null, boolean $readOnly = false )
Constructor. Initializes the list with an array or an iterable object.
Constructor. Initializes the list with an array or an iterable object.
Parameters
- $data
array $data the initial data. Default is null, meaning no initialization.
- $readOnly
boolean $readOnly whether the list is read-only
Throws
CException
If data is not null and neither an array nor an iterator.
|
public
boolean
|
#
getReadOnly( )
Returns
boolean whether this list is read-only or not. Defaults to false.
|
protected
|
#
setReadOnly( boolean $value )
Parameters
- $value
boolean $value whether this list is read-only or not
|
public
Iterator
|
#
getIterator( )
Returns an iterator for traversing the items in the list. This method is
required by the interface IteratorAggregate.
Returns an iterator for traversing the items in the list. This method is
required by the interface IteratorAggregate.
Returns
Iterator
an iterator for traversing the items in the list.
Implementation of
|
public
integer
|
#
count( )
Returns the number of items in the list. This method is required by Countable
interface.
Returns the number of items in the list. This method is required by Countable
interface.
Returns
integer number of items in the list.
Implementation of
|
public
integer
|
#
getCount( )
Returns the number of items in the list.
Returns the number of items in the list.
Returns
integer the number of items in the list
|
public
mixed
|
#
itemAt( integer $index )
Returns the item at the specified offset. This method is exactly the same as
CList::offsetGet() .
Returns the item at the specified offset. This method is exactly the same as
CList::offsetGet() .
Parameters
- $index
integer $index the index of the item
Returns
mixed the item at the index
Throws
|
public
integer
|
#
add( mixed $item )
Appends an item at the end of the list.
Appends an item at the end of the list.
Parameters
- $item
mixed $item new item
Returns
integer the zero-based index at which the item is added
|
public
|
#
insertAt( integer $index, mixed $item )
Inserts an item at the specified position. Original item at the position and
the next items will be moved one step towards the end.
Inserts an item at the specified position. Original item at the position and
the next items will be moved one step towards the end.
Parameters
- $index
integer $index the specified position.
- $item
mixed $item new item
Throws
CException
If the index specified exceeds the bound or the list is read-only
|
public
integer
|
#
remove( mixed $item )
Removes an item from the list. The list will first search for the item. The
first item found will be removed from the list.
Removes an item from the list. The list will first search for the item. The
first item found will be removed from the list.
Parameters
- $item
mixed $item the item to be removed.
Returns
integer the index at which the item is being removed
Throws
|
public
mixed
|
#
removeAt( integer $index )
Removes an item at the specified position.
Removes an item at the specified position.
Parameters
- $index
integer $index the index of the item to be removed.
Returns
mixed the removed item.
Throws
CException
If the index specified exceeds the bound or the list is read-only
|
public
|
#
clear( )
Removes all items in the list.
Removes all items in the list.
|
public
boolean
|
#
contains( mixed $item )
Parameters
- $item
mixed $item the item
Returns
boolean whether the list contains the item
|
public
integer
|
#
indexOf( mixed $item )
Parameters
- $item
mixed $item the item
Returns
integer the index of the item in the list (0 based), -1 if not found.
|
public
array
|
#
toArray( )
Returns
array the list of items in array
|
public
|
#
copyFrom( mixed $data )
Copies iterable data into the list. Note, existing data in the list will be
cleared first.
Copies iterable data into the list. Note, existing data in the list will be
cleared first.
Parameters
- $data
mixed $data the data to be copied from, must be an array or object implementing
Traversable
Throws
CException
If data is neither an array nor a Traversable.
|
public
|
#
mergeWith( mixed $data )
Merges iterable data into the map. New data will be appended to the end of
the existing data.
Merges iterable data into the map. New data will be appended to the end of
the existing data.
Parameters
- $data
mixed $data the data to be merged with, must be an array or object implementing
Traversable
Throws
CException
If data is neither an array nor an iterator.
|
public
boolean
|
#
offsetExists( integer $offset )
Returns whether there is an item at the specified offset. This method is
required by the interface ArrayAccess.
Returns whether there is an item at the specified offset. This method is
required by the interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to check on
Returns
boolean
Implementation of
|
public
mixed
|
#
offsetGet( integer $offset )
Returns the item at the specified offset. This method is required by the
interface ArrayAccess.
Returns the item at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to retrieve item.
Returns
mixed the item at the offset
Throws
Implementation of
|
public
|
#
offsetSet( integer $offset, mixed $item )
Sets the item at the specified offset. This method is required by the
interface ArrayAccess.
Sets the item at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to set item
- $item
mixed $item the item value
Implementation of
|
public
|
#
offsetUnset( integer $offset )
Unsets the item at the specified offset. This method is required by the
interface ArrayAccess.
Unsets the item at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to unset item
Implementation of
|