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

查看:112
本文介绍了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)

推荐答案

Arrays 和列表(由 List<T> 表示)及其子类型 MutableList<T> )有很多差异,以下是最重要的差异:

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

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

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

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

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<T>是可变的(可以通过对其进行任何引用进行更改),但是List<T>没有修改方法(它是不可变列表实现).

  • 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<T>具有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
    

  • Array<T> T 不变(Array<Int>不是Array<Number>),与MutableList<T>相同,但是List<T>是协变的(List<Int>List<Number>).

  • 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<Int>映射到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.

    List<T>MutableList<T>映射类型,并且在Java互操作性方面具有特殊的行为(从Kotlin中将Java的List<T>视为List<T>MutableList<T>).数组也已映射,但它们具有其他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<String>和具有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.

    关于用法,优良作法是在代码的性能关键部分以外的任何地方都优先使用列表而不是数组,其理由与

    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天全站免登陆