Good morning StackOverflow community!
I'm learning to program in Kotlin and a doubt has arisen regarding its basic concepts, specifically regarding arrays and lists.
Lists based on what I've researched can be both mutable and immutable, however arrays can only be immutable. Taking into account that lists have a practically identical behavior to arrays, then:
1) Why do arrays exist in Kotlin?
2) Why not always use lists?
3) Are there specific cases in which an array is more convenient than a list?
I have a lot of these questions because trying to do a similarity in PHP, for example, there are arrays and objects. Both fulfill particular functions and even when both are naturally mutable, it is possible to differentiate according to the level of complexity of the issue being dealt with when to choose one and when to choose another.
In Kotlin I can't define any important difference that motivates me to ever use arrays instead of lists.
Thank you very much for the collaboration you can give me! All the best.
Note: I have used the word array instead of array because I am more used to it.
Why do arrays exist in kotlin?
Java Compatibility
Note that Kotlin is designed for maximum compatibility with Java, so the inclusion of Arrays has a lot to do with their existence in Java.
Arrays have a clear implementation
An array is a clear and defined type: a fixed-length container of elements of the same type, accessed by index, and mutable.
A list is a specification: a behavior is defined (a collection of elements of the same type that can be iterated, that can access elements given an index -not necessarily direct access by index-...
Immutable arrays and lists have different generic behavior
And this point is complicated, because in Java an array is not generic . Kotlin engineers decided to remove primitives altogether in order to have a more rounded and stable type system, and in their effort they further blurred the differences between lists and arrays.
If you look at the doc article on generics, you'll find that it differentiates between immutable arrays and lists on variance. It is a somewhat complex topic that would lengthen this answer a lot, so it will be enough for me to tell you in kotlin,
Array<Int>
it is not a subtypeArray<Number>
, whileList<Int>
it is a subtype ofList<Number>
Arrays have specific types for their primitive counterparts in Java.
Aside from the generic type
Array<T>
, Kotlin includes specific types that translate literally to their primitive counterparts, such asIntArray
orDoubleArray
.Its correspondence, taking an array of integers as an example, is as follows:
IntArray
-int[]
Array<Int>
-Integer[]
In summary
The differences between arrays and lists are very blurred in kotlin, since the decisions the engineers made to do away with primitive types and split lists into mutable and immutable brought the two types very close together. They have certain differences, but they are not very noticeable and the lists almost always win.
Why not always use lists?
Why not? In Java, lists were introduced later than arrays and offer a huge improvement in utility. In Effective Java, Joshua Bloch recommended using lists on top of arrays whenever possible, and this is how it has been done so far.
Are there specific cases where an array is more convenient than a list?
There are cases, for example, if you know that you are going to work with a fixed number of elements and you want to access and change those elements. But it's an unlikely case, and the mutable list can do the job just fine, as well as allowing you to switch implementations later if you prefer.
An array should be used when you know the fixed size in advance that you will need or you know it dynamically.
A list should be used when you don't know the size, either it will fluctuate very often (especially in the central positions that are the most expensive to modify), or it can become very large at some times and at other times very small. small (memory savings vs. always having a huge array), either you are going to delete objects and you don't want/need to keep empty positions (because you can't delete positions in the array, or you can't afford to create a new array with less positions and copy the interesting content).
After that, each language can have its own implementations/interfaces and have a series of specific arrays and lists for each purpose, as is the case of Java with the Java Collections Framework .
Note: Kotlin runs on top of the JVM (Java Virtual Machine).