0
0
Kotlinprogramming~15 mins

List creation (listOf, mutableListOf) in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
List creation (listOf, mutableListOf)
📖 Scenario: You are organizing a small party and need to keep track of the guests who will attend.
🎯 Goal: Create a list of guests using listOf and then create a mutable list using mutableListOf to add more guests later.
📋 What You'll Learn
Create an immutable list called guests with exactly these names: "Alice", "Bob", "Charlie"
Create a mutable list called moreGuests starting with the same names as guests
Add the name "Diana" to the moreGuests list
Print the moreGuests list to show all guests
💡 Why This Matters
🌍 Real World
Lists are used everywhere to keep track of groups of items, like guests at a party, tasks to do, or products in a store.
💼 Career
Knowing how to create and modify lists is a basic skill for any programmer, useful in almost all software development jobs.
Progress0 / 4 steps
1
Create an immutable list of guests
Create an immutable list called guests using listOf with these exact names: "Alice", "Bob", "Charlie".
Kotlin
Need a hint?

Use val guests = listOf("Alice", "Bob", "Charlie") to create the list.

2
Create a mutable list from the immutable list
Create a mutable list called moreGuests using mutableListOf and initialize it with the contents of the guests list.
Kotlin
Need a hint?

Use mutableListOf().apply { addAll(guests) } to copy the guests list.

3
Add a new guest to the mutable list
Add the name "Diana" to the moreGuests mutable list using the add method.
Kotlin
Need a hint?

Use moreGuests.add("Diana") to add the new guest.

4
Print the final list of guests
Print the moreGuests list to show all the guests including the newly added one.
Kotlin
Need a hint?

Use println(moreGuests) to print the list.