list 如何在 Kotlin 中将项目添加到列表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55488291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add an item to a list in Kotlin?
提问by Mohit Lakhanpal
I'm trying to add an element list to the list of string, but I found Kotlin
does not have an add function like java
so please help me out how to add the items to the list.
我正在尝试将元素列表添加到字符串列表中,但我发现Kotlin
没有类似的添加功能java
,请帮助我了解如何将项目添加到列表中。
class RetrofitKotlin : AppCompatActivity() {
var listofVechile:List<Message>?=null
var listofVechileName:List<String>?=null
var listview:ListView?=null
var progressBar:ProgressBar?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_retrofit_kotlin)
listview=findViewById<ListView>(R.id.mlist)
var apiInterfacee=ApiClass.client.create(ApiInterfacee::class.java)
val call=apiInterfacee.getTaxiType()
call.enqueue(object : Callback<TaxiTypeResponse> {
override fun onResponse(call: Call<TaxiTypeResponse>, response: Response<TaxiTypeResponse>) {
listofVechile=response.body()?.message!!
println("Sixze is here listofVechile ${listofVechile!!.size}")
if (listofVechile!=null) {
for (i in 0..listofVechile!!.size-1) {
//how to add the name only listofVechileName list
}
}
//println("Sixze is here ${listofVechileName!!.size}")
val arrayadapter=ArrayAdapter<String>(this@RetrofitKotlin,android.R.layout.simple_expandable_list_item_1,listofVechileName)
listview!!.adapter=arrayadapter
}
override fun onFailure(call: Call<TaxiTypeResponse>, t: Throwable) {
}
})
}
}
回答by Kevin Coppock
A more idiomatic approach would be to use MutableList
instead of specifically ArrayList
. You can declare:
更惯用的方法是使用MutableList
而不是专门使用ArrayList
. 您可以声明:
val listOfVehicleNames: MutableList<String> = mutableListOf()
And add to it that way. Alternatively, you may wish to prefer immutability, and declare it as:
并以这种方式添加它。或者,您可能希望更喜欢不变性,并将其声明为:
var listOfVehicleNames: List<String> = emptyList()
And in your completion block, simply reassign it:
在您的完成块中,只需重新分配它:
listOfVehicleNames = response.body()?.message()?.orEmpty()
.map { it.name() /* assumes name() function exists */ }
回答by Radesh
If you don't want or can't use array list directly use this code for add item
如果您不想或不能直接使用数组列表,请使用此代码添加项目
itemsList.toMutableList().add(item)
itemlist: list of your items
itemlist: 你的物品清单
item: item you want to add
item: 要添加的项目
回答by Peppe S
you should use a MutableList like ArrayList
你应该使用像 ArrayList 这样的 MutableList
var listofVechileName:List<String>?=null
becomes
变成
var listofVechileName:ArrayList<String>?=null
and with that you can use the method add
并且你可以使用方法添加
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/add.html
回答by Brent
instead of using a regular list which is immutable just use an arrayListof which is mutable
而不是使用不可变的常规列表,只需使用可变的 arrayListof
so your regular list will become
所以你的常规列表将变成
var listofVehicleNames = arrayListOf("list items here")
then you can use the add function
然后你可以使用添加功能
listOfVehicleNames.add("what you want to add")
回答by Jonik
Talking about an idiomatic approach...
谈论一种惯用的方法......
When you can get away with only using immutablelists (which means usuallyin Kotlin), simply use +
or plus
. It returns a new list
with all elements of the original list plus the newly added one:
当您只能使用不可变列表(通常在 Kotlin 中)时,只需使用+
或plus
。它返回一个新列表,其中包含原始列表的所有元素加上新添加的元素:
val original = listOf("orange", "apple")
val modified = original + "lemon" // [orange, apple, lemon]
original.plus("lemon")
yields the same result as original + "lemon"
. Slightly more verbose but might come in handy when combining several collection operations:
original.plus("lemon")
产生与 相同的结果original + "lemon"
。稍微冗长一些,但在组合多个集合操作时可能会派上用场:
return getFruit()
.plus("lemon")
.distinct()
Besides adding a single element, you can use plus
to concatenate a whole collection too:
除了添加单个元素之外,您还可以使用plus
连接整个集合:
val original = listOf("orange", "apple")
val other = listOf("banana", "strawberry")
val newList = original + other // [orange, apple, banana, strawberry]
Disclaimer: this doesn't directly answer OP's question, but I feel that in a question titled "How to add an item to a list in Kotlin?", which is a top Google hit for this topic, plus
mustbe mentioned.
免责声明:这并没有直接回答 OP 的问题,但我觉得在题为“如何在 Kotlin 中将项目添加到列表中?”的问题中,plus
必须提及该主题的 Google 热门话题。