🎓 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'll dive deep into the listOf() function, exploring its syntax, usage, and providing a a lot's of examples for your better understanding.
What is listOf()?
In Kotlin, the listOf() function is a part of the standard library, and it's used to create an immutable list. This means that once the list is created using listOf(), you cannot add, remove, or modify elements in the list.
Basic Syntax:
val list: List<Type> = listOf(element1, element2, element3, ...)Examples with Outputs
Creating a Simple List of Integers
val numbers = listOf(1, 2, 3, 4, 5)
println(numbers) // Output: [1, 2, 3, 4, 5]Creating a List of Strings
val fruits = listOf("Apple", "Banana", "Cherry")
println(fruits) // Output: [Apple, Banana, Cherry]List with Mixed Data Types
In Kotlin, you can create a list of mixed data types, although it's not a common practice.
val mixed = listOf(1, "Apple", 2.5, 'A')
println(mixed) // Output: [1, Apple, 2.5, A]Empty List
You can also create an empty list using listOf(). The type of the list can be inferred from the context or explicitly mentioned.
val emptyList = listOf<String>()
println(emptyList) // Output: []Accessing Elements from the List
Just like any list in Kotlin, you can access elements by their index.
val colors = listOf("Red", "Green", "Blue")
println(colors[1]) // Output: GreenList of Lists
Yes, you can have lists inside a list!
val matrix = listOf(
listOf(1, 2, 3),
listOf(4, 5, 6),
listOf(7, 8, 9)
)
println(matrix[1][2]) // Output: 6Caveats
fruits.add("Orange") // Error: Unresolved reference: addConclusion
In this guide, we understood how to use Kotlin's listOf() function with lots of examples. So in Kotlin, listOf() is a handy utility function used to create an immutable list of elements.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