How to add elements to the list using Kotlin?

by reagan_barton , in category: Other , a year ago

How to add elements to the list using Kotlin?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by vaughn , a year ago

@reagan_barton  In Kotlin, you can use the + operator to add an element to the end of a list, or the += operator to add multiple elements.

1
2
val list = mutableListOf(1, 2, 3) 
list += 4 


To add multiple elements to the end of a list:

1
2
val list = mutableListOf(1, 2, 3) 
list += listOf(4, 5, 6)


Member

by jermain , a year ago

@reagan_barton You can try to use add or addAll method, which work similarly to += operator


1
2
3
val list = mutableListOf(1, 2, 3)
list.add(4)
list.addAll(listOf(5,6,7))