🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, we will learn about the Kotlin mutableListOf() function with lots of examples.
What is mutableListOf()?
The mutableListOf() function in Kotlin is a utility function that allows you to create a mutable list. As opposed to the read-only lists created using listOf(), mutable lists can be modified after their creation, allowing elements to be added, removed, or updated.
Basic Syntax:
val mutableList: MutableList<Type> = mutableListOf(element1, element2, element3, ...)Examples with Outputs
Creating a Basic Mutable List of Integers
val numbers = mutableListOf(1, 2, 3)
println(numbers) // Output: [1, 2, 3]Adding Elements to the List
val animals = mutableListOf("Dog", "Cat")
animals.add("Bird")
println(animals) // Output: [Dog, Cat, Bird]Removing Elements from the List
val colors = mutableListOf("Red", "Blue", "Green")
colors.remove("Blue")
println(colors) // Output: [Red, Green]Modifying Elements in the List
val fruits = mutableListOf("Apple", "Banana", "Cherry")
fruits[1] = "Mango"
println(fruits) // Output: [Apple, Mango, Cherry]Merging Two Mutable Lists
val list1 = mutableListOf(1, 2, 3)
val list2 = mutableListOf(4, 5, 6)
list1.addAll(list2)
println(list1) // Output: [1, 2, 3, 4, 5, 6]Clearing All Elements from the List
val data = mutableListOf("A", "B", "C")
data.clear()
println(data) // Output: []Convert a mutableListOf() to an immutable list and vice versa
val mutableList = mutableListOf(1, 2, 3)
val immutableList = mutableList.toList()
val backToMutable = immutableList.toMutableList()Conclusion
In Kotlin, mutableListOf() is a standard library function that provides a dynamic way to create lists whose elements can be altered – you can add, remove, or modify elements as per your requirements.
Related Kotlin Posts
- Kotlin List
- Kotlin Set
- Kotlin listOf
- Kotlin mutableListOf
- Kotlin mapOf
- Kotlin mutableMapOf
- Kotlin setOf
- Kotlin mutableSetOf
- Kotlin sequenceOf
- Kotlin list filter
- Kotlin list map
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment