Kotlin Arrays

Kotlin Tutorial

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:

0, 1, 4, 9, 16

Here i represents the index.


How to Access Array Elements

Arrays use index numbers starting from 0.


 

Index positions:

IndexValue
0Red
1Green
2Blue

How to Modify Array Elements

Arrays in Kotlin are mutable (you can change values).


 

Output:

10, 50, 30

Array Properties and Functions

size


indices


joinToString()

Output:

1, 2, 3

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.

TypeArray Type
IntIntArray
DoubleDoubleArray
FloatFloatArray
BooleanBooleanArray
CharCharArray

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.

FeatureArrayList
Fixed sizeYesUsually dynamic
MutableYesList / MutableList
Built-in methodsLimitedMore functional APIs
Recommended forLow-level useMost 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:

1, 2, 5, 9

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 IntArray instead of Array<Int> for better performance.

  • Use List if 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.

You may also like...