【问题标题】:Implementing one's own linked ArrayList in Java用Java实现自己的链接ArrayList
【发布时间】:2016-11-18 06:26:00
【问题描述】:

我正在考虑使用 addindexOfremove 等方法实现我自己的链接 ArrayList。但是,我似乎不太明白 set 函数如何返回泛型在特定索引处替换旧对象的对象。

类似的东西

public E set(int index, E element
{
    E elementAtIndex;
    //some code
    return elementAtIndex;
}

有人可以帮助解释set 方法的伪代码吗?

【问题讨论】:

  • “链接数组列表”这个词很容易混淆,因为“链接”和“数组”通常是互斥的。您的问题也令人困惑,因为 set() 方法的文档非常清楚。你不明白哪一部分?我们无法在您的伪代码中解释 // some code,因为只有您知道您想要它做什么。
  • 嗯,我正在尝试实现自己的类,而不是使用内置文档,这就是我要求伪代码的原因。

标签: java arraylist linked-list set


【解决方案1】:

首先,“linked”前缀与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

}

希望这会有所帮助。 :)

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 2021-09-01
    • 2014-05-10
    • 1970-01-01
    • 2011-06-07
    • 2017-10-26
    • 2014-09-03
    • 2011-07-02
    • 2015-11-11
    相关资源
    最近更新 更多