实施一个';在Java中创建自己的链接ArrayList
我正在考虑用实施一个';在Java中创建自己的链接ArrayList,java,arraylist,linked-list,set,Java,Arraylist,Linked List,Set,我正在考虑用add、indexOf、remove等方法实现我自己的链接ArrayList。然而,我似乎不太明白set函数如何返回在特定索引处替换旧对象的通用对象 类似于 public E set(int index, E element { E elementAtIndex; //some code return elementAtIndex; } 有人能解释一下set方法的psuedocode吗?首先,“linked”前缀与ArrayList无关,因为它已经明确定义了
add
、indexOf
、remove
等方法实现我自己的链接ArrayList。然而,我似乎不太明白set
函数如何返回在特定索引处替换旧对象的通用对象
类似于
public E set(int index, E element
{
E elementAtIndex;
//some code
return elementAtIndex;
}
有人能解释一下set
方法的psuedocode吗?首先,“linked”前缀与ArrayList无关,因为它已经明确定义了迭代顺序,这基本上是索引的递增顺序。它可能与HashMap相关,就像LinkedHashMap一样
关于set方法的psuedocode解释
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
}
public E集(int索引,E新值){
如果索引<0或索引>=listSize//请检查索引是否在范围内
然后抛出IndexOutOfBoundsException//抛出适当的异常
oldValue=backingElementArray[index]//将旧值临时存储在索引处
backingElementArray[index]=newValue//用新值替换旧值
返回旧值//返回旧值
}
希望这有帮助。:) 术语“链接数组列表”令人困惑,因为“链接”和“数组”通常是互斥的。您的问题也令人困惑,因为
set()
方法的文档非常清楚。你不明白哪一部分?我们无法解释您的伪代码中的一些代码,因为只有您知道您希望它做什么。我正在尝试实现自己的类,而不是使用内置文档,这就是为什么我要求使用psuedocode。