English 中文(简体)
Kotlin - 将列表转换为 MutableList 的最普通方式
原标题:Kotlin - Most idiomatic way to convert a List to a MutableList
I have a method (getContacts) that returns a List and I need to convert this result to a MutableList. Currently the best way I can think of doing it is like this: val contacts: MutableList = ArrayList(presenter.getContacts()) Is there a more idiomatic/"less Java" way to do that?
最佳回答
Consider using the toMutableList() function: presenter.getContacts().toMutableList() There are toMutableList() extensions for the stdlib types that one might want to convert to a mutable list: Collection, Iterable, Sequence, CharSequence, Array and primitive arrays.
问题回答
If you only wants ArrayList, you can create your own Kotlin extension. fun List.toArrayList(): ArrayList{ return ArrayList(this) } Then you can use it in your application like myList.toArrayList() Simple and easy
You can also use toTypedArray() if you want to convert a List or MutableList to Array /** * Returns a *typed* array containing all of the elements of this collection. * * Allocates an array of runtime type `T` having its size equal to the size of this collection * and populates the array with the elements of this collection. * @sample samples.collections.Collections.Collections.collectionToTypedArray */ @Suppress("UNCHECKED_CAST") public actual inline fun Collection.toTypedArray(): Array { @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") val thisCollection = this as java.util.Collection return thisCollection.toArray(arrayOfNulls(0)) as Array } Example myMutableList.toTypedArray()
You can use Kotlin s generic function with a return MutableList type: fun kotlinMutable(list: List): MutableList { return list.toMutableList() } fun main() { val list: List = listOf("a","b","c","x") val kml = kotlinMutable(list) kml.add("y") // [a, b, c, x, y] for (i in 2 downTo 0 step 1) { kml.remove(kml[i]) } println(kml) // [x, y] }




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签