How are Kotlin Array's toList and asList different?

后端 未结 2 1843
青春惊慌失措
青春惊慌失措 2020-12-28 15:40

The Kotlin Array class offers asList(), toList(), and toMutableList() methods. The first two methods both return a

2条回答
  •  猫巷女王i
    2020-12-28 16:24

    Basically asList() still maintains a reference to the original Array. That means mutations to that list will also mutate the underlying Array.

    toList() is simply copying the values of the Array into a new List, but there is no lingering link afterwards.

    For most use-cases, they probably are interchangeable. asList() will likely have slightly better performance (since it isn't performing a copy) and toList() will be a "safe" copy against unexpected mutations.

提交回复
热议问题