2016-11-18 63 views
-1

我一直在尋找實現自己的聯繫與ArrayList的方法,如addindexOfremove等。不過,我不能完全似乎明白瞭如何set函數返回的通用對象在特定的索引處取代舊的。在Java中實現了自己的聯繫的ArrayList

沿

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

東西線有人能幫助解釋爲set方法的僞代碼?

+2

術語「鏈接數組列表」令人困惑,因爲「鏈接」和「數組」通常是互斥的。你的問題也令人困惑,因爲'set()'方法的文檔非常清晰。你不明白哪部分?我們不能在你的僞代碼中解釋'//一些代碼',因爲只有你知道你想要它做什麼。 –

+0

嗯,我試圖實現我自己的類,而不是使用內置的文檔,這就是爲什麼我要求psuedocode。 –

回答

0

首先,「鏈接」前綴與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 

} 

希望這有助於。 :)