我写了一个数组包装类 PersonArray,它可以包含某种类型的对象(人)。每个人都有一个唯一的 getHash() 函数,它返回 ID + Name 作为唯一标识符。这允许从 PersonArray 中快速检索 Person。PersonArray 实际上包含两个内部数组。一种用于存储 Person 对象($items),另一种用于存储 Hash 值($itemsHash)。
我想创建一个 insertAt(index, Person) 函数,它将 Person 对象放在 $items 数组中的 [index] 位置。有没有办法在数组中的某个位置插入?如果是这样,我怎样才能更新 PersonArray 的 $itemsHash?
class Person {
function getHash() {
return $this->id . $this->name;
}
}
class PersonArray implements Iterator {
public $items = array();
public $itemsHash = array();
public function Find($pKey) {
if($this->ContainsKey($pKey)) {
return $this->Item($this->internalRegisteredHashList[$pKey]);
}
}
public function Add($object) {
if($object->getHash()) {
$this->internalRegisteredHashList[$object->getHash()] = $this->Count();
array_push($this->items, $object);
}
}
public function getItems() {
return $this->items;
}
function ContainsKey($pKey) {}
function Count() {}
function Item($pKey) {}
//Iteration implementation
public function rewind() {}
public function current() {}
public function key() {}
public function next() {}
public function valid() {}
}