Question:
Good morning StackOverflow community!
I am learning to program in Kotlin and I have had a question regarding its basic concepts, specifically in regards to arrays and lists.
Lists based on what I have investigated can be both mutable and immutable, however arrays can only be immutable. Taking into account that the lists have a practically identical operation to the arrays, then:
1) Why do fixes exist in Kotlin?
2) Why not always use lists?
3) Are there specific cases in which an arrangement is more convenient than a list?
I have a lot of these questions because trying to make a similarity in PHP for example arrays and objects exist. Both fulfill particular functions and even though both are naturally mutable, it can be differentiated according to the level of complexity of the matter at hand, when to choose one and when to choose another.
In Kotlin I can't define any major differences that would motivate me to ever use arrays instead of lists.
Thank you very much for the collaboration you can give me! All the best.
Answer:
Note: I have used the word array instead of array because I am more used to it
Why do fixes exist in kotlin?
Java compatibility
Note that Kotlin is designed for maximum Java compatibility, 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 which is 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 the elements given an index -not necessarily direct access by index- …
Arrays and Immutable Lists Have Different Generic Behavior
And this point is complicated, because in Java an array is not generic . The Kotlin engineers decided to eliminate primitives entirely for a rounder and more 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 will find that it differs between arrays and immutable lists in variance. It is a somewhat complex topic that would make this answer much longer so it will be enough for me to tell you in kotlin, Array<Int>
is not an Array<Number>
subtype, while List<Int>
is a subtype of List<Number>
Arrays have specific types for their primitive counterparts in Java
Aside from the generic Array<T>
, Kotlin includes specific types that translate literally to their primitive counterpart, such as IntArray
or DoubleArray
.
Its correspondence, taking an array of integers as an example, is the following:
-
IntArray
–int[]
-
Array<Int>
–Integer[]
In summary
The differences between arrays and lists are very blurred in kotlin, since the decisions engineers made to kill primitive types and divide lists into mutable and immutable brought the two types very close. 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 usability. In Effective Java, Joshua Bloch recommended using lists on top of arrays whenever possible, and this is how it has been done so far.
The existence of arrays in kotlin is probably a residue of compatibility, and they offer very little advantage in exchange for little flexibility.
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 is an unlikely case and the mutable list can perfectly fulfill this function, as well as allowing you to change your implementation later if you prefer.