简体   繁体   English

Kotlin 中列表和数组类型的区别

[英]Difference between List and Array types in Kotlin

What is the difference between List and Array types? ListArray类型有什么区别?
It seems can make same operations with them (loops, filter expression, etc..), is there any 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)

Arrays and lists (represented by List<T> and its subtype MutableList<T> ) have many differences, here are the most significant ones: 数组和列表(由List<T>及其子类型MutableList<T> )有很多不同之处,以下是最重要的:

  • Array<T> is a class with known implementation: it's a sequential fixed-size memory region storing the items (and on JVM it is represented by Java array ). Array<T>是一个具有已知实现的类:它是一个连续的固定大小的内存区域来存储项目(在 JVM 上它由Java array表示)。

    List<T> and MutableList<T> are interfaces which have different implementations: ArrayList<T> , LinkedList<T> etc. Memory representation and operations logic of lists are defined in concrete implementation, eg indexing in a LinkedList<T> goes through the links and takes O(n) time whereas ArrayList<T> stores its items in a dynamically allocated array. List<T>MutableList<T>是接口,它们有不同的实现: ArrayList<T>LinkedList<T>等。 列表的内存表示和操作逻辑在具体实现中定义,例如LinkedList<T>索引经过链接并花费 O(n) 时间,而ArrayList<T>将其项目存储在动态分配的数组中。

     val list1: List<Int> = LinkedList<Int>() val list2: List<Int> = ArrayList<Int>()
  • Array<T> is mutable (it can be changed through any reference to it), but List<T> doesn't have modifying methods (it is either read-only view of MutableList<T> or an immutable list implementation ). Array<T>是可变的(可以通过对它的任何引用进行更改),但List<T>没有修改方法(它是MutableList<T>只读视图不可变列表实现)。

     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 fixed size and cannot expand or shrink retaining identity (you need to copy an array to resize it).数组具有固定大小,不能扩展或收缩保留标识(您需要复制数组以调整其大小)。 As to the lists, MutableList<T> has add and remove functions, so that it can increase and reduce its size.对于列表, MutableList<T>具有addremove功能,因此可以增加和减小其大小。

     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 invariant on T ( Array<Int> is not Array<Number> ), the same for MutableList<T> , but List<T> is covariant ( List<Int> is List<Number> ). Array<T>T不变的Array<Int>不是Array<Number> ),对于MutableList<T> ,但List<T>是协变的( List<Int>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. which are mapped to Java primitive arrays ( int[] , double[] , char[] ), not boxed ones ( Array<Int> is mapped to Java's Integer[] ).阵列被用于原语优化:有单独IntArrayDoubleArrayCharArray等,其被映射到Java原始阵列( int[] double[] char[]盒装酮( Array<Int>被映射到Java的Integer[] )。 Lists in general do not have implementations optimized for primitives, though some libraries (outside JDK) provide primitive-optimized lists.列表通常没有针对原语优化的实现,尽管一些库(JDK 之外)提供了原语优化的列表。

  • List<T> and MutableList<T> are mapped types and have special behaviour in Java interoperability (Java's List<T> is seen from Kotlin as either List<T> or MutableList<T> ). List<T>MutableList<T>映射类型,在 Java 互操作性中具有特殊行为(Java 的List<T>在 Kotlin 中被视为List<T>MutableList<T> )。 Arrays are also mapped, but they have other rules of Java interoperability.数组也被映射,但它们具有 Java 互操作性的其他规则

  • Certain array types are used in annotations (primitive arrays, Array<String> , and arrays with enum class entries), and there's a special array literal syntax for annotations .某些数组类型用于注释(原始数组、 Array<String>和具有enum class条目的数组),并且annotations有一个特殊的数组文字语法 Lists and other collections cannot be used in annotations.不能在注释中使用列表和其他集合。

  • As to the usage, good practice is to prefer using lists over arrays everywhere except for performance critical parts of your code, the reasoning is the same to that for Java .至于用法,好的做法是除了代码的性能关键部分外,更喜欢在任何地方使用列表而不是数组,推理与Java相同。

The major difference from usage side is that Arrays have a fixed size while (Mutable)List can adjust their size dynamically.与使用方面的主要区别在于Arrays具有固定大小,而(Mutable)List可以动态调整其大小。 Moreover Array is mutable whereas List is not.此外Array是可变的,而List不是。

Furthermore kotlin.collections.List is an interface implemented among others by java.util.ArrayList .此外kotlin.collections.List是一个由java.util.ArrayList实现的接口。 It's also extended by kotlin.collections.MutableList to be used when a collection that allows for item modification is needed.它也由kotlin.collections.MutableList扩展,以在需要允许项目修改的集合时使用。

On the jvm level, Array is represented by arrays .在 jvm 级别, Arrayarrays表示。 List on the other hand is represented by java.util.List since there are no immutable collections equivalents available in Java.另一方面, Listjava.util.List表示,因为 Java 中没有可用的不可变集合等价物。

In additional to the above, identity comparison is also different:除了上述之外,身份比较也不同:

val l1 = listOf("a")
val l2 = listOf("a")
var x = (l1 == l2) // => true

val a1 = arrayOf("a")
val a2 = arrayOf("a")
var y = (a1 == a2) // => false

**general ,the different between List and Array types is: ** **一般来说,List和Array类型之间的不同之处是:**

List<...>:

for only read. 仅供阅读。

Array<...>:

you can modify it,or add something. 你可以修改它,或添加一些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM