Design Converter
Education
Last updated on Jan 2, 2025
Last updated on Jan 2, 2025
What’s the difference between arrays and lists in Kotlin?
If you’ve been coding with Kotlin, you’ve probably used both arrays and lists but may have wondered when to choose one over the other.
In this blog, we’ll break down Kotlin Array vs List so you can understand their key differences and best use cases.
Keep reading to find the right option for your projects!
A list in Kotlin is an ordered collection of elements that can be either mutable or immutable. Lists are part of the Kotlin standard library and are widely used due to their flexibility and ease of use. In a Kotlin list, you can store elements of the same type or, with a generic type parameter, elements of different types. Mutable lists are often preferred in scenarios requiring dynamic resizing, frequent insertion, removal, or modifications.
In Kotlin, lists come in two flavors:
listOf
. You cannot modify their contents after initialization.1fun main() { 2 val immutableList = listOf("Apple", "Banana", "Cherry") 3 println(immutableList[0]) // Accessing the first element 4}
mutableListOf
. You can insert elements, update elements, or remove elements as needed.1fun main() { 2 val mutableList = mutableListOf("Apple", "Banana") 3 mutableList.add("Cherry") // Adding a new element 4 println(mutableList) // Output: [Apple, Banana, Cherry] 5}
List<String?>
).An array in Kotlin is a fundamental data structure that stores a fixed-size sequence of elements in contiguous memory locations. Arrays are optimized for performance and are a good choice when the size and type of elements are known in advance, especially in performance-critical applications.
Unlike lists, arrays have a fixed size, meaning you must specify the number of elements at the time of creation. Additionally, all elements in an array must be of the same type.
Example of creating an array of strings:
1fun main() { 2 val array = arrayOf("Apple", "Banana", "Cherry") 3 println(array.size) // Output: 3 4}
1fun main() { 2 val array = arrayOf("Apple", "Banana", "Cherry") 3 println(array[1]) // Accessing the second element: Output: Banana 4}
1fun main() { 2 val intArray = intArrayOf(1, 2, 3, 4) 3 println(intArray[2]) // Accessing the third element: Output: 3 4}
1fun main() { 2 val array = arrayOf("Apple", "Banana", "Cherry") 3 array[1] = "Blueberry" // Updating an element 4 println(array.joinToString()) // Output: Apple, Blueberry, Cherry 5}
Immutable lists, created using the listOf
function, cannot be modified after creation:
1fun main() { 2 val immutableList = listOf("Apple", "Banana", "Cherry") 3 println(immutableList[0]) // Access the first element 4}
Mutable lists, created using the mutableListOf
function, allow adding, removing, or modifying elements. You can also specify the type explicitly, e.g., mutableListOf<String>("Apple", "Banana")
:
1fun main() { 2 val mutableList = mutableListOf("Apple", "Banana") 3 mutableList.add("Cherry") // Adding an element 4 mutableList.removeAt(1) // Removing the second element 5 println(mutableList) // Output: [Apple, Cherry] 6}
arrayOf
Function:1fun main() { 2 val array = arrayOf("Apple", "Banana", "Cherry") 3 println(array.size) // Output: 3 4}
1fun main() { 2 val intArray = intArrayOf(1, 2, 3) 3 println(intArray[1]) // Output: 2 4}
1fun main() { 2 val array = Array(5) { it * 2 } // Creates an array [0, 2, 4, 6, 8] 3 println(array.joinToString()) // Output: 0, 2, 4, 6, 8 4}
1fun main() { 2 val intArray = intArrayOf(1, 2, 3, 4) 3 println(intArray[2]) // Output: 3 4}
1fun main() { 2 val mutableList = mutableListOf(1, 2, 3, 4) 3 mutableList.add(5) 4 println(mutableList) // Output: [1, 2, 3, 4, 5] 5}
Feature | List | Array |
---|---|---|
Size | Dynamic (can grow/shrink) | Fixed size |
Type of Elements | Same or mixed (via generics) | Must be of the same type |
Memory Usage | Higher (object references) | Lower (primitive types) |
Resizing | Supports adding/removing | Not resizable |
Access Speed | Slightly slower | Faster (contiguous memory) |
Null Elements | Allowed if nullable | Allowed if nullable |
Flexibility | More flexible | Less flexible |
Common Operations | Search, filter, sort | Direct manipulation |
Both lists and arrays are integral to Kotlin’s collection types, with lists providing flexibility for dynamic operations and arrays excelling in performance-critical tasks with fixed datasets.
Choose wisely based on your application needs. Happy coding!
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.