CList

优质
小牛编辑
133浏览
2023-12-01
所有包 | 属性 | 方法
system.collections
继承class CList » CComponent
实现IteratorAggregate, Traversable, ArrayAccess, Countable
子类CFilterChain, CTypedList
源自1.0
版本$Id: CList.php 3430 2011-11-02 23:10:03Z alexander.makarow@gmail.com $
源码framework/collections/CList.php
CList实现一个整数索引的集合类。

你可以通过使用 itemAt,add,insertAt,remove,以及removeAt访问,添加,插入,删除项目。 通过getCount,你可以取得这个列表的项目数。 如下所示,CList也可以当作一般数组来使用:
$list[]=$item;  //在尾部添加
$list[$index]=$item; //$index一定要在0和$list->Count之间
unset($list[$index]); //删除指定$index的项目
if(isset($list[$index])) //如果列表中存在$index指定的项目
foreach($list as $index=>$item) //遍历列表中的项目
$n=count($list); //返回列表中的项目数


通过重写insertAt(),以为removeAt()To extend CList by doing additional operations with each addition or removal 你可以在拥有添加/删除操作的基础上扩展额外的操作(如类型检查)。

公共属性

隐藏继承属性

属性类型描述定义在
countinteger返回列表的项目数。CList
iteratorIterator返回遍历这个项目列表的迭代器。CList
readOnlyboolean返回值说明这个列表是否为只读。默认为false。CList

公共方法

隐藏继承方法

方法描述定义在
__call()如果类中没有调的方法名,则调用这个方法。CComponent
__construct()构造方法。CList
__get()返回一个属性值、一个事件处理程序列表或一个行为名称。CComponent
__isset()检查一个属性是否为null。CComponent
__set()设置一个组件的属性值。CComponent
__unset()设置一个组件的属性为null。CComponent
add()在列表尾部添加一个项目。CList
asa()返回这个名字的行为对象。CComponent
attachBehavior()附加一个行为到组件。CComponent
attachBehaviors()附加一个行为列表到组件。CComponent
attachEventHandler()为事件附加一个事件处理程序。CComponent
canGetProperty()确定属性是否可读。CComponent
canSetProperty()确定属性是否可写。CComponent
clear()删除列表中所有项目。CList
contains()CList
copyFrom()将迭代器中的数据复制到列表。CList
count()返回列表的项目数。CList
detachBehavior()从组件中分离一个行为。CComponent
detachBehaviors()从组件中分离所有行为。CComponent
detachEventHandler()分离一个存在的事件处理程序。CComponent
disableBehavior()禁用一个附加行为。CComponent
disableBehaviors()禁用组件附加的所有行为。CComponent
enableBehavior()启用一个附加行为。CComponent
enableBehaviors()启用组件附加的所有行为。CComponent
evaluateExpression()计算一个PHP表达式,或根据组件上下文执行回调。CComponent
getCount()返回列表的项目数。CList
getEventHandlers()返回一个事件的附加处理程序列表。CComponent
getIterator()返回遍历这个项目列表的迭代器。CList
getReadOnly()返回返回值说明这个列表是否为只读。默认为false。CList
hasEvent()确定一个事件是否定义。CComponent
hasEventHandler()检查事件是否有附加的处理程序。CComponent
hasProperty()确定属性是否被定义。CComponent
indexOf()CList
insertAt()将项目插入到指定位置。CList
itemAt()返回指定位置的项目。CList
mergeWith()将迭代器的数据整合到mapCList
offsetExists()返回值说明列表中是否包含这个项目。CList
offsetGet()返回指定位置的项目。CList
offsetSet()在指定位置插入项目。CList
offsetUnset()删除指定位置的项目。CList
raiseEvent()发起一个事件。CComponent
remove()从列表中删除一个项目。CList
removeAt()删除指定位置的项目。CList
toArray()CList

受保护方法

隐藏继承方法

方法描述定义在
setReadOnly()设置设置这个列表是否为只读CList

属性详细

count 属性 只读 public integer getCount()

返回列表的项目数。

iterator 属性 只读 public Iterator getIterator()

返回遍历这个项目列表的迭代器。 此方法为接口IteratorAggregate强制要求实现。

readOnly 属性 public boolean getReadOnly()
protected void setReadOnly(boolean $value)

返回值说明这个列表是否为只读。默认为false。

方法详细

__construct() 方法
public void __construct(array $data=NULL, boolean $readOnly=false)
$dataarray初始化的数据。默认为null,意味着没有初始化。
$readOnlyboolean标识这个列表是否为只读。
源码: framework/collections/CList.php#61 (显示) publicfunction__construct($data=null,$readOnly=false)
{
if($data!==null)
$this->copyFrom($data);
$this->setReadOnly($readOnly);
}

构造方法。 根据数组或者迭代对象初始化这个列表。

add() 方法
public integer add(mixed $item)
$itemmixed新的项目
{return}integer返回在加入该项目的从零开始的索引
源码: framework/collections/CList.php#136 (显示) publicfunctionadd($item)
{
$this->insertAt($this->_c,$item);
return$this->_c-1;
}

在列表尾部添加一个项目。

clear() 方法
public void clear()
源码: framework/collections/CList.php#221 (显示) publicfunctionclear()
{
for($i=$this->_c-1;$i>=0;--$i)
$this->removeAt($i);
}

删除列表中所有项目。

contains() 方法
public boolean contains(mixed $item)
$itemmixed项目对象
{return}boolean返回值说明列表中是否包含这个项目。
源码: framework/collections/CList.php#231 (显示) publicfunctioncontains($item)
{
return$this->indexOf($item)>=0;
}
copyFrom() 方法
public void copyFrom(mixed $data)
$datamixed要复制的数据, 只能是数组或者继承于Traversable的对象。
源码: framework/collections/CList.php#262 (显示) publicfunctioncopyFrom($data)
{
if(is_array($data)||($datainstanceofTraversable))
{
if($this->_c>0)
$this->clear();
if($datainstanceofCList)
$data=$data->_d;
foreach($dataas$item)
$this->add($item);
}
elseif($data!==null)
thrownewCException(Yii::t('yii','ListdatamustbeanarrayoranobjectimplementingTraversable.'));
}

将迭代器中的数据复制到列表。 注意,列表中已经存在的数据会被首先删除。

count() 方法
public integer count()
{return}integer返回列表的项目数。
源码: framework/collections/CList.php#99 (显示) publicfunctioncount()
{
return$this->getCount();
}

返回列表的项目数。 此方法为接口Countable强制要求实现。

getCount() 方法
public integer getCount()
{return}integer返回列表的项目数
源码: framework/collections/CList.php#108 (显示) publicfunctiongetCount()
{
return$this->_c;
}

返回列表的项目数。

getIterator() 方法
public Iterator getIterator()
{return}Iterator返回用来遍历列表中的项目的迭代器。
源码: framework/collections/CList.php#89 (显示) publicfunctiongetIterator()
{
returnnewCListIterator($this->_d);
}

返回遍历这个项目列表的迭代器。 此方法为接口IteratorAggregate强制要求实现。

getReadOnly() 方法
public boolean getReadOnly()
{return}boolean返回值说明这个列表是否为只读。默认为false。
源码: framework/collections/CList.php#71 (显示) publicfunctiongetReadOnly()
{
return$this->_r;
}
indexOf() 方法
public integer indexOf(mixed $item)
$itemmixed项目对象
{return}integer返回项目在列表中的索引值(基于0),-1表示没有找到。
源码: framework/collections/CList.php#240 (显示) publicfunctionindexOf($item)
{
if(($index=array_search($item,$this->_d,true))!==false)
return$index;
else
return-1;
}
insertAt() 方法
public void insertAt(integer $index, mixed $item)
$indexinteger指定位置。
$itemmixed新的项目
源码: framework/collections/CList.php#150 (显示) publicfunctioninsertAt($index,$item)
{
if(!$this->_r)
{
if($index===$this->_c)
$this->_d[$this->_c++]=$item;
elseif($index>=0&&$index<$this->_c)
{
array_splice($this->_d,$index,0,array($item));
$this->_c++;
}
else
thrownewCException(Yii::t('yii','Listindex"{index}"isoutofbound.',
array('{index}'=>$index)));
}
else
thrownewCException(Yii::t('yii','Thelistisreadonly.'));
}

将项目插入到指定位置。 该位置的原项目和 它后面直到列表结束的项目会被删除。

itemAt() 方法
public mixed itemAt(integer $index)
$indexinteger项目的索引值
{return}mixed索引对应的项目。
源码: framework/collections/CList.php#120 (显示) publicfunctionitemAt($index)
{
if(isset($this->_d[$index]))
return$this->_d[$index];
elseif($index>=0&&$index<$this->_c)//incasethevalueisnull
return$this->_d[$index];
else
thrownewCException(Yii::t('yii','Listindex"{index}"isoutofbound.',
array('{index}'=>$index)));
}

返回指定位置的项目。 这个方法跟offsetGet是完全一样的。

mergeWith() 方法
public void mergeWith(mixed $data)
$datamixed要合并的数据, 只能是数组或者继承于Traversable的对象.
源码: framework/collections/CList.php#283 (显示) publicfunctionmergeWith($data)
{
if(is_array($data)||($datainstanceofTraversable))
{
if($datainstanceofCList)
$data=$data->_d;
foreach($dataas$item)
$this->add($item);
}
elseif($data!==null)
thrownewCException(Yii::t('yii','ListdatamustbeanarrayoranobjectimplementingTraversable.'));
}

将迭代器的数据整合到map 新的数据会添加到已存在的数据后面。

offsetExists() 方法
public boolean offsetExists(integer $offset)
$offsetinteger要检查的位置
{return}boolean
源码: framework/collections/CList.php#302 (显示) publicfunctionoffsetExists($offset)
{
return($offset>=0&&$offset<$this->_c);
}

返回值说明列表中是否包含这个项目。 此方法为接口ArrayAccess强制要求实现。

offsetGet() 方法
public mixed offsetGet(integer $offset)
$offsetinteger要插入项目的索引。
{return}mixed返回该索引的项目。
源码: framework/collections/CList.php#314 (显示) publicfunctionoffsetGet($offset)
{
return$this->itemAt($offset);
}

返回指定位置的项目。 此方法为接口ArrayAccess强制要求实现。

offsetSet() 方法
public void offsetSet(integer $offset, mixed $item)
$offsetinteger要设置项目的位置
$itemmixed项目对象
源码: framework/collections/CList.php#325 (显示) publicfunctionoffsetSet($offset,$item)
{
if($offset===null||$offset===$this->_c)
$this->insertAt($this->_c,$item);
else
{
$this->removeAt($offset);
$this->insertAt($offset,$item);
}
}

在指定位置插入项目。 此方法为接口ArrayAccess强制要求实现。

offsetUnset() 方法
public void offsetUnset(integer $offset)
$offsetinteger要删除的项目。
源码: framework/collections/CList.php#341 (显示) publicfunctionoffsetUnset($offset)
{
$this->removeAt($offset);
}

删除指定位置的项目。 此方法为接口ArrayAccess强制要求实现。

remove() 方法
public integer remove(mixed $item)
$itemmixed要删除的项目的索引。
{return}integer返回已经删除的项目的索引。
源码: framework/collections/CList.php#177 (显示) publicfunctionremove($item)
{
if(($index=$this->indexOf($item))>=0)
{
$this->removeAt($index);
return$index;
}
else
returnfalse;
}

从列表中删除一个项目。 这个列表首先会搜索这个项目。 在列表中第一个找到的项目会被删除。(也就是说, 列表中可能存在着相同的项目)

removeAt() 方法
public mixed removeAt(integer $index)
$indexinteger要删除的项目的索引。
{return}mixed返回已经删除的项目。
源码: framework/collections/CList.php#194 (显示) publicfunctionremoveAt($index)
{
if(!$this->_r)
{
if($index>=0&&$index<$this->_c)
{
$this->_c--;
if($index===$this->_c)
returnarray_pop($this->_d);
else
{
$item=$this->_d[$index];
array_splice($this->_d,$index,1);
return$item;
}
}
else
thrownewCException(Yii::t('yii','Listindex"{index}"isoutofbound.',
array('{index}'=>$index)));
}
else
thrownewCException(Yii::t('yii','Thelistisreadonly.'));
}

删除指定位置的项目。

setReadOnly() 方法
protected void setReadOnly(boolean $value)
$valueboolean设置这个列表是否为只读
源码: framework/collections/CList.php#79 (显示) protectedfunctionsetReadOnly($value)
{
$this->_r=$value;
}
toArray() 方法
public array toArray()
{return}array数组中的项目列表
源码: framework/collections/CList.php#251 (显示) publicfunctiontoArray()
{
return$this->_d;
}