I try to implement `LinkedList<E>` (from Jav...
# multiplatform
r
I try to implement
LinkedList<E>
(from Java), but what Kotlin list should I extend?
ArrayList
is final, so can't use that, and just
List
makes me implement every method myself 😞 extending
MutableList
is even worse....
d
AbstractList
?
âž• 1
r
Thank you! That's what I was looking for. Now it forces me to implement
get(index: Int)
(and for mutable list a few more). Where do I get/save those values? Just create another list in a property of it?
d
What values? (Also, I recommend extending
AbstractCollection
for a linked list).
r
The values for the list....
It wants me to implement
get(index)
, what should I return....
d
Return the element at that index in the list.
r
Obviously, but how to get that, as I can't use
this.get(index)
as that's the method I am implement, where is the data stored?
d
You need to implement that part, you decide
Consider extending
AbstractMutableList
, it will make more sense because you'll need to implement
add
and stuff.
You should not forget to override
iterator
as otherwise your iterator will be extremely slow, something like O(n2/2)
r
But to save the data, I have to implement another list right? But eventually there has to be a list that saves data by itself instead of wrapping another list. Wonder how this list saves its data
d
Look at the source for
java.util.LinkedList
k
You can use in native, but it’s designed to work multithreaded. Performance was not a huge concern
👌 1