CHAPTER 10
Beginner
Arrays and Collections
Updated: May 18, 2026
5 min read
# CHAPTER 10
Arrays and Collections
1. Chapter Introduction
When building a shopping app, you don't createitem1, item2, and item3 variables. You create a single list that holds *all* items. In this chapter, we will explore Collections. Kotlin provides several ways to group data: Arrays (fixed size), Lists (ordered items), Sets (unique items), and Maps (key-value pairs). Crucially, Kotlin enforces strict differences between *Mutable* (changeable) and *Immutable* (read-only) collections.
2. Learning Objectives
By the end of this chapter, you will be able to:- Create and manipulate standard Arrays.
- Understand the difference between Immutable and Mutable Collections.
-
Use
listOf()andmutableListOf().
-
Use Sets (
setOf) to ensure uniqueness.
-
Use Maps (
mapOf) for Key-Value pairs.
3. Arrays (Fixed Size)
An Array is a container that holds a fixed number of values of a single type. Once an array is created, its size cannot grow or shrink.
kotlin
4. Collections: Immutable vs Mutable
In Java, standard Lists can always be modified (add/remove). This can lead to bugs if a list is passed to a function that secretly deletes items! Kotlin splits collections into two distinct interfaces:-
1.
Immutable (Read-Only): You can read items, but you cannot add, remove, or change them. (e.g.,
listOf()).
-
2.
Mutable: You can add, remove, and change items. (e.g.,
mutableListOf()).
5. Lists (Ordered Collections)
Lists are the most common collection. They preserve the order of items and allow duplicates.#### Read-Only List
kotlin
#### Mutable List
kotlin
6. Sets (Unique Collections)
A Set is a collection that contains no duplicate elements. The order is generally not guaranteed.
kotlin
7. Maps (Key-Value Pairs)
A Map (often called a Dictionary in other languages) stores data in pairs: aKey and a Value. Keys must be unique, but values can be duplicated.
kotlin
8. Mini Project: Student Marks Manager
Let's build a program using a Map to store student grades.
kotlin
9. Common Mistakes
-
Confusing
valwith Immutability: If you declareval list = mutableListOf("A"), the *variable reference* cannot change (you can't assign a whole new list to it), but you CAN still modify the contents (list.add("B")).valprotects the reference;listOf()protects the contents!
-
IndexOutOfBounds: Trying to access
numbers[5]in an array that only has 3 items will crash the program.
10. Best Practices
-
Default to Read-Only: Always use
listOf()by default. Only upgrade tomutableListOf()if your specific business logic requires adding or removing items. Passing read-only lists between functions guarantees data integrity.
11. Exercises
-
1.
Create a
mutableListOfcontaining three names.
- 2. Add a fourth name to the list.
- 3. Remove the first name.
-
4.
Use a
forloop to print all remaining names.
12. MCQs with Answers
Question 1
What is the defining characteristic of an Array in Kotlin?
Question 2
What is the fundamental difference between listOf() and mutableListOf()?
Question 3
Which collection type strictly prohibits duplicate elements?
Question 4
What is the output of setOf(1, 1, 2, 3)?
Question 5
Which collection type stores data in Key-Value pairs?
Question 6
What keyword is used to link a Key to a Value when initializing a Map?
val list = mutableListOf(1, 2), can you do list.add(3)?
a) Yes, because the list contents are mutable, even though the variable reference val is fixed b) No, val makes the contents immutable
Answer: a) Yes.
Question 8
How do you access the value associated with the key "Bob" in a map?
Question 9
What happens if you try to call .add() on a collection created with listOf()?
Question 10
Why does Kotlin enforce a split between Mutable and Immutable collections?
13. Interview Questions
-
Q: Explain the difference between
val myVarandlistOf(). (Answer:valprevents the variable *reference* from being reassigned.listOf()prevents the *contents* of the collection from being altered).
-
Q: When would you use a
Setinstead of aList?
14. Summary
Collections are essential for handling dynamic data. Kotlin improves upon Java's collections by strictly separating read-only and mutable interfaces. By defaulting to immutable lists (listOf()), Kotlin developers write code that is inherently safer, preventing hidden bugs caused by functions unexpectedly modifying shared data structures.