Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Kotlin's MutableList add() function always add to the end of the list?

Tags:

kotlin

I am adding an element into a MutableList and I want to know its index.

mutableList.add(foo)

Will the index of the most recently added element always be the last index mutableList.size - 1 like it is with ArrayList?

I don't want to use mutableList.indexOf(foo) because I believe it takes O(n). I'm not finding lot of documentation on these.

like image 582
BeLambda Avatar asked Jan 02 '23 02:01

BeLambda


1 Answers

Yes, MutableList.add() always adds to the end of the list (this is a requirement for any class implementing the interface). I've filed an issue to say this explicitly in the documentation.

like image 133
yole Avatar answered Jan 05 '23 02:01

yole