In addition to `mutableList.add(0,element),` are t...
# announcements
l
In addition to
mutableList.add(0,element),
are there any other APIs to insert an element to the head of a
mutablelist
?
mutableList.add(0,element)
is ok, but not as expressive. 🤔
c
What's the issue with
add(0, element)
?
r
I think he means it's expensive. (eg, you basically need to copy whole list to do it) Well, it depends on what exactly you are doing.
add(0, e)
is not expensive by it's definition, but by implementation of MutableList - for example LinkedList has effective implementation of it (but sacrifices a lot of other functions) There are several solutions, depending on what you want to be doing: a) LinkedList, (but get and stuff is usually way more important, and linked list sucks at nearly everything else) b) reverse, add N elements at end, reverse - if you want to add lot of elements at start at once, this is simple and basically as effective as adding at the end c) "Circle List"/Array dequeue, not sure if there is some implementation of it, but you can implement something very similar to ArrayList, but it can push/pop from both front and back very effectively. For the c) thing, as I'm not entirely sure about it's official name here is simple explanation: You store a simple array, index of start, and index of end of the data. If you want to access an element at
index
, you look at
(start + index) modulo total_array_size
→ eg, after last element of array, you consider next at the first position. (unless it's the last index) This way, you will have an allocated memory both before & after real data, so you can do push/pop simply with manipulating the stored start/end indices and storing one element. If
real size == total size
you can simply allocated array double the size and copy the whole real data to it → similar trick to what ArrayList uses. Note that REALLY consider what are you doing, as usually it's not worth to do such optimizations in real-world application, unless this manipulation is on your "hot codepath" - the thing that does the vast majority of total work
👍 1
d
Yes,
ArrayDeque
is in the Kotlin common stdlib
r
Yep, that's the one I meant! Hard to remember which structures are called what in 5+ languages 😛 (oh I actually wrote that name lel, I just thought up the name myself right now, so it looks similar to kotlin/java naming)
l
Sorry my bad, I should describe it more clearly, but still thanks for the response. “Expressive” means more readable. Let’s assume I have a mutableList, and I want to add an element to the head of it. I can write the following code using the existing API.
Copy code
mutableList.apply {
            // do some calculations
            val element = .....
            add(0, element)
        }
        .xxx { }
It works fine. But I’m wondering if there’s some other APIs like this:
Copy code
mutableList.insertElementToHead {
            // do some calculations
            val element = ...
            element
        }
            .xxx {}
It doesn’t seem like there are so many differences between the two ways. But I prefer the second way, when other people read the code, they can know what I do at the first glance. Maybe I’m kind of paranoid? Of course I can write an extension function for the MutableList to do this, but just in case there are other existing ways.
s
you are being paranoid. Having too many APIs comes with its own issues. Take Groovy or Scala.
read the code, they can know what I do at the first glance
depends on who is reading the code. Most of the programmer won't have issues in understanding
add(0, item)
means
addFirst(item)
. Sure, you can have
addFirst
APIs. But for normal List, its not needed specifically. Things like
Deque
provides them, because there it makes sense to have a special function which is doing
add(0, item)
👍 1
❤️ 1