Kotlin 中列表和数组类型的区别 [英] Difference between List and Array types in Kotlin

查看:25
本文介绍了Kotlin 中列表和数组类型的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ListArray 类型有什么区别?
似乎可以对它们进行相同的操作(循环,过滤器表达式等.),在行为或用法上有什么不同吗?

What is the difference between List and Array types?
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)

推荐答案

数组 和列表(由 List 表示) 及其子类型 MutableList) 有很多不同之处,以下是最重要的:

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

  • Array<T> 是一个具有已知实现的类:它是一个存储项目的顺序固定大小的内存区域(在 JVM 上它由 Java 数组).

  • 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).

ListMutableList 是具有不同实现的接口:ArrayListLinkedList<;T> 等,具体实现中定义了列表的内存表示和操作逻辑,例如LinkedList 中的索引遍历链接并花费 O(n) 时间,而 ArrayList 将其项存储在动态分配的数组中.

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, e.g. 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.

val list1: List<Int> = LinkedList<Int>()
val list2: List<Int> = ArrayList<Int>()

  • Array 是可变的(可以通过对它的任何引用来更改),但是 List 没有修改方法(它是只读视图MutableList不可变列表实现).

  • 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).

    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
    

  • 数组大小固定,不能扩展或缩小保留标识(您需要复制数组以调整其大小).对于列表,MutableList具有addremove功能,可以增加和减少其大小.

  • 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.

    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
    

  • ArrayT(Array 不是 Array),MutableList 也一样code>,但 List 是协变的(ListList).

  • 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>).

    val a: Array<Number> = Array<Int>(0) { 0 } // won't compile
    val l: List<Number> = listOf(1, 2, 3) // OK
    

  • 数组针对基元进行了优化:有单独的 IntArrayDoubleArrayCharArray 等映射到 Java 基元数组(int[]double[]char[]),而不是 装箱(Array 映射到 Java 的 Integer[]>).列表通常没有针对原语优化的实现,尽管一些库(JDK 之外)提供了原语优化列表.

  • 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[]). Lists in general do not have implementations optimized for primitives, though some libraries (outside JDK) provide primitive-optimized lists.

    ListMutableList映射类型 并且在 Java 互操作性方面具有特殊行为(Java 的 List 在 Kotlin 中被视为 List<;T>MutableList).数组也被映射,但它们具有 Java 互操作性的其他规则.

    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>). Arrays are also mapped, but they have other rules of Java interoperability.

    某些数组类型用于注释(原始数组、Array,以及带有 enum class 条目的数组),还有一个特殊的 注解的数组字面量语法.不能在注释中使用列表和其他集合.

    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. Lists and other collections cannot be used in annotations.

    至于用法,好的做法是除了代码的性能关键部分外,更喜欢在任何地方使用列表而不是数组,推理与 Java 的.

    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.

    这篇关于Kotlin 中列表和数组类型的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆