Difference between List and Array types in Kotlin

What is the difference between List and Array types?
It seems that the same operations (loops, filter expression, etc.) can be performed with them. Is there a difference in behavior or usage?

 val names1 = listOf("Joe","Ben","Thomas") val names2 = arrayOf("Joe","Ben","Thomas") for (name in names1) println(name) for (name in names2) println(name) 
+85
kotlin
Mar 28 '16 at 12:27
source share
3 answers

Arrays and lists (represented by List<T> and its subtype MutableList<T> ) have many differences, here are the most significant of them:

  • Array<T> is a class with a well-known implementation: it is a sequential fixed-size memory area in which elements are stored (and in the JVM it is represented by a Java array ).

    List<T> and MutableList<T> are interfaces that have different implementations: ArrayList<T> , LinkedList<T> , etc. The representation in memory and the logic of the operations of lists are defined in a particular implementation, for example, indexing is performed on LinkedList<T> links and takes O (n) time, while ArrayList<T> stores its elements in a dynamically allocated array.

     val list1: List<Int> = LinkedList<Int>() val list2: List<Int> = ArrayList<Int>() 
  • Array<T> is mutable (it can be changed using any reference to it), but List<T> has no modifying methods (this is either a MutableList<T> MutableList<T> only for MutableList<T> or an implementation of an immutable list ).

     val a = arrayOf(1, 2, 3) a[0] = a[1] // OK val l = listOf(1, 2, 3) l[0] = l[1] // doesn't compile val m = mutableListOf(1, 2, 3) m[0] = m[1] // OK 
  • Arrays have a fixed size and cannot expand or reduce the persisting identity (you need to copy the array to resize it). As for lists, MutableList<T> has add and remove functions, so that it can increase and decrease its size.

     val a = arrayOf(1, 2, 3) println(a.size) // will always be 3 for this array val l = mutableListOf(1, 2, 3) l.add(4) println(l.size) // 4 
  • Array<T> is an invariant for T ( Array<Int> not Array<Number> ), the same is for MutableList<T> , but List<T> is covariant ( List<Int> is List<Number> ).

     val a: Array<Number> = Array<Int>(0) { 0 } // won't compile val l: List<Number> = listOf(1, 2, 3) // OK 
  • Arrays are optimized for primitives: there are separate IntArray , DoubleArray , CharArray , etc. DoubleArray , CharArray mapped to arrays of Java primitives ( int[] , double[] , char[] ), and not boxed ( Array<Int> mapped to Java Integer[] ). Lists generally do not have implementations optimized for primitives, although some libraries (outside the JDK) provide lists optimized for primitives.

  • List<T> and MutableList<T> are mapped types and have special behavior in compatibility with Java (Java List<T> considered by Kotlin as List<T> or MutableList<T> ). Arrays are also displayed, but they have other rules for Java interaction.

  • Certain types of arrays are used in annotations (primitive arrays, Array<String> and arrays with enum class entries), and there is a special array literal syntax for annotations . Lists and other collections cannot be used in annotations.

  • As far as usage is concerned, it is good practice to prefer to use lists over arrays everywhere, with the exception of parts of your code that are critical in terms of performance, the argument is the same as for Java .

+148
Mar 28 '16 at 13:50
source share

The main difference from using the side is that Arrays have a fixed size, and the (Mutable)List can dynamically resize them. Moreover, Array is mutable, while List not.

Moreover kotlin.collections.List is an interface implemented among other java.util.ArrayList . It also extends kotlin.collections.MutableList , which will be used when you need collections that allow you to modify an element.

At jvm level, Array is represented by arrays . List on the other hand, is represented by java.util.List , since Java does not have immutable collection equivalents.

+15
Mar 28 '16 at 12:40
source share

** In general, the differences between List and Array types: **

 List<...>: 

only for reading.

 Array<...>: 

You can change this, or add something.

0
Jan 17 '19 at 22:51
source share



All Articles