It provides a backwards view of the underlying list. Changes affecting the size of the list, for example, automatically cause any views that were previously returned by invoking subList to have undefined behavior.įor a common case, which is looking at a list backwards, the Kotlin standard library also comes with the asReversed function. To reiterate all of that works because these aren’t two different collections – there is only one collection, and subList has just given us a different perspective on that list!Īn important note on the topic of sublists: They are only well-defined as long as the underlying, original list is not structurally changed. That also includes other lists and mutable lists – where you’ll get a fresh copy when calling toMutableList: And, wherever you were able to find a toList method, as discussed previously, you’ll probably also find a toMutableList. Once again, mutable lists are commonly created via the mutableListOf function, with a bunch of values as arguments. It's precisely that extra functionality that we’re interested in right now! If we consult an excerpt of a class hierarchy, we can see that MutableList specializes List, meaning everything we’ve learned about lists so far also works for their mutable counterpart, plus some extra functionality. What's so special about mutable lists? Well, you can mutate them! That, of course, doesn’t mean that these lists will turn into zombies (□♂️), but that you can change their content. But let’s put that on the back burner for a bit, and move on to a special kind of list – it's time to talk about mutable lists! Enter fullscreen mode Exit fullscreen modeĪs you may suspect, this list of list features is not quite exhaustive – as always, there’s some more to explore even on this subject.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |