我正在考虑使用add
,indexOf
,remove
等方法实现我自己的链接ArrayList。但是,我似乎无法理解如何{{{ 1}}返回在特定索引处替换旧对象的通用对象的函数。
的内容
set
有人可以帮助解释public E set(int index, E element
{
E elementAtIndex;
//some code
return elementAtIndex;
}
方法的伪代码吗?
答案 0 :(得分:0)
首先,"链接" prefix与ArrayList无关,因为它已经明确定义了迭代顺序,这基本上是其索引的递增顺序。它可能与HashMap相关,就像LinkedHashMap一样。
对set方法进行伪代码解释,
public E set (int index, E newValue){
If index < 0 or index >= listSize // check whether the index is within the range
then throw IndexOutOfBoundsException // throw appropriate exception
oldValue = backingElementArray[index] // temporarily store the old value at the index
backingElementArray[index] = newValue // replace the old value with new one
return oldValue // return the old value
}
希望这会有所帮助。 :)