Kotlin Arrays

Kotlin Arrays – Complete Beginner Guide With Examples
Kotlin arrays are one of the most fundamental concepts in programming. Whether you’re building Android apps, backend services, or learning Kotlin as your first language, understanding arrays is essential.
In this beginner-friendly, SEO-optimized guide, you’ll learn:
What arrays are in Kotlin
How to create and initialize arrays
How to access and modify elements
Array properties and useful functions
Looping through arrays
Primitive arrays in Kotlin
Array vs List differences
Real-world examples
Common mistakes beginners make
Let’s start from the basics
What Is an Array in Kotlin?
An array is a data structure that stores multiple values of the same type in a single variable.
Instead of creating multiple variables:
You can store them in one array:
Now all values are stored inside numbers.
Why Use Arrays?
Arrays are useful when:
You need to store multiple related values
You want to process a collection of data
You need indexed access
You want better memory organization
Example use cases:
Storing student marks
Storing product prices
Storing temperature readings
Managing user IDs
How to Create an Array in Kotlin
Using arrayOf()
This is the most common way.
Kotlin automatically detects the type as Array<String>.
Specify Type Explicitly
Create Empty Array
This creates an array with 5 null values.
Using Constructor
Output:
Here i represents the index.
How to Access Array Elements
Arrays use index numbers starting from 0.
Index positions:
| Index | Value |
|---|---|
| 0 | Red |
| 1 | Green |
| 2 | Blue |
How to Modify Array Elements
Arrays in Kotlin are mutable (you can change values).
Output:
Array Properties and Functions
size
indices
joinToString()
Output:
contains()
Looping Through Arrays
Loop Through an Array with a For Loop
Access Elements via Array Indices
Iterate Using the forEach Function
Primitive Type Arrays in Kotlin
Kotlin provides specialized arrays for primitive types to improve performance.
| Type | Array Type |
|---|---|
| Int | IntArray |
| Double | DoubleArray |
| Float | FloatArray |
| Boolean | BooleanArray |
| Char | CharArray |
Example: IntArray
Why use IntArray?
Because Array<Int> stores objects, while IntArray stores primitive values — which is more memory efficient.
Multi-Dimensional Arrays
Kotlin supports 2D arrays.
Example:
This is useful for:
Game boards
Mathematical matrices
Table data
Array vs List in Kotlin
Many beginners confuse arrays with lists.
| Feature | Array | List |
|---|---|---|
| Fixed size | Yes | Usually dynamic |
| Mutable | Yes | List / MutableList |
| Built-in methods | Limited | More functional APIs |
| Recommended for | Low-level use | Most general use |
In modern Kotlin, List is used more frequently than Array.
Example:
Real-World Example – Student Marks
This is a practical beginner example.
Sorting Arrays
Output:
Converting Array to List
Common Beginner Mistakes
Index Out Of Bounds
Always check size before accessing.
Confusing Array<Int> with IntArray
They are different in performance and usage.
Forgetting Zero-Based Indexing
Arrays start from 0, not 1.
Performance Considerations
Use
IntArrayinstead ofArray<Int>for better performance.Use
Listif you need flexible size.Avoid unnecessary copying of arrays.
When Should You Use Arrays?
Use arrays when:
Size is fixed
You need fast indexed access
You are working with performance-critical code
You are handling low-level data
Frequently Asked Questions (FAQs)
1. Are arrays mutable in Kotlin?
Yes, arrays are mutable. You can modify their elements.
2. What is the difference between Array and IntArray?
Array<Int> stores objects.IntArray stores primitive integers and is more efficient.
3. Can array size change in Kotlin?
No. Arrays have fixed size. Use MutableList for dynamic size.
4. How do I loop through an array?
Using for, indices, or forEach.
5. Is List better than Array?
In most modern Kotlin applications, List is preferred.
Conclusion
Kotlin arrays are a foundational concept that every developer must understand.
You learned:
How to create arrays
How to access and modify elements
Primitive arrays
Multi-dimensional arrays
Array vs List differences
Real-world examples
Common mistakes
Mastering arrays will make your Kotlin journey much smoother.
