Apache flex ActionScript-3:阵列与阵列列表
谁能告诉我什么更快:数组还是数组列表?(行动脚本3) 我试图找到一个关于这个的页面,但什么也没找到。 多谢各位 是IList的一个简单实现,它使用一个备份数组作为数据源。可以使用Apache flex ActionScript-3:阵列与阵列列表,apache-flex,actionscript-3,Apache Flex,Actionscript 3,谁能告诉我什么更快:数组还是数组列表?(行动脚本3) 我试图找到一个关于这个的页面,但什么也没找到。 多谢各位 是IList的一个简单实现,它使用一个备份数组作为数据源。可以使用IList界面的方法和属性访问和操作支持数组中的项。对ArrayList实例的操作修改数据源;例如,如果在ArrayList上使用removietemat()方法,则从基础数组中删除该项 显然,ArrayList类包装了一个数组对象——因此普通数组比ArrayList对象快。数组可能稍快一点,或者两者相等。ArrayLi
IList
界面的方法和属性访问和操作支持数组中的项。对ArrayList
实例的操作修改数据源;例如,如果在ArrayList
上使用removietemat()
方法,则从基础数组中删除该项
显然,ArrayList类包装了一个数组对象——因此普通数组比ArrayList对象快。数组可能稍快一点,或者两者相等。ArrayList是iList的一个实现,它使用。。。数组作为备份对象。如前所述,数组速度更快。事实上,它要快几个数量级
数组访问的等价物是getItemAt
和setItemAt
实施:
public function getItemAt(index:int, prefetch:int = 0):Object
{
if (index < 0 || index >= length)
{
var message:String = resourceManager.getString(
"collections", "outOfBounds", [ index ]);
throw new RangeError(message);
}
return source[index];
}
公共函数getItemAt(索引:int,预取:int=0):对象
{
如果(索引<0 | |索引>=长度)
{
var消息:String=resourceManager.getString(
“集合”、“边界外”、“索引”);
抛出新的RangeError(消息);
}
返回源[索引];
}
以及:
公共函数setItemAt(项:对象,索引:int):对象
{
如果(索引<0 | |索引>=长度)
{
var消息:String=resourceManager.getString(
“集合”、“边界外”、“索引”);
抛出新的RangeError(消息);
}
var oldItem:对象=源[索引];
来源[索引]=项目;
StopTrackUpdate(旧项);
开始跟踪更新(项目);
//发送适当的事件
如果(_dispatchEvents==0)
{
var hasCollectionListener:布尔=
hasEventListener(CollectionEvent.COLLECTION\u更改);
var hasPropertyListener:布尔=
hasEventListener(PropertyChangeEvent.PROPERTY\u CHANGE);
var updateInfo:PropertyChangeEvent;
if(hasCollectionListener | | hasPropertyListener)
{
updateInfo=新的PropertyChangeEvent(PropertyChangeEvent.PROPERTY\u CHANGE);
updateInfo.kind=PropertyChangeEventKind.UPDATE;
updateInfo.oldValue=oldItem;
updateInfo.newValue=项目;
updateInfo.property=索引;
}
if(hasCollectionListener)
{
var事件:CollectionEvent=
新CollectionEvent(CollectionEvent.COLLECTION\u更改);
event.kind=CollectionEventKind.REPLACE;
event.location=索引;
event.items.push(updateInfo);
调度事件(事件);
}
if(hasPropertyListener)
{
dispatchEvent(更新信息);
}
}
返回旧项目;
}
这里有很多电话和检查。请注意,\u dispatchEvents==0
在默认情况下是true
(除非您disableEvents
),因此,实际上编写是一个巨大的操作
然而,ArrayList
确实提供了很多功能,这些功能在flex中非常有用。一个好的组合是获取底层的数组
(可通过ArrayList::source
访问),对操作进行配置,然后重新分配它(假设有侦听器观察该数组)
另外,如果您使用FlashPlayer10,那么Vector的性能将优于Array
格里茨
back2dos您是否做了一些测试用例来看看什么更快?
public function setItemAt(item:Object, index:int):Object
{
if (index < 0 || index >= length)
{
var message:String = resourceManager.getString(
"collections", "outOfBounds", [ index ]);
throw new RangeError(message);
}
var oldItem:Object = source[index];
source[index] = item;
stopTrackUpdates(oldItem);
startTrackUpdates(item);
//dispatch the appropriate events
if (_dispatchEvents == 0)
{
var hasCollectionListener:Boolean =
hasEventListener(CollectionEvent.COLLECTION_CHANGE);
var hasPropertyListener:Boolean =
hasEventListener(PropertyChangeEvent.PROPERTY_CHANGE);
var updateInfo:PropertyChangeEvent;
if (hasCollectionListener || hasPropertyListener)
{
updateInfo = new PropertyChangeEvent(PropertyChangeEvent.PROPERTY_CHANGE);
updateInfo.kind = PropertyChangeEventKind.UPDATE;
updateInfo.oldValue = oldItem;
updateInfo.newValue = item;
updateInfo.property = index;
}
if (hasCollectionListener)
{
var event:CollectionEvent =
new CollectionEvent(CollectionEvent.COLLECTION_CHANGE);
event.kind = CollectionEventKind.REPLACE;
event.location = index;
event.items.push(updateInfo);
dispatchEvent(event);
}
if (hasPropertyListener)
{
dispatchEvent(updateInfo);
}
}
return oldItem;
}