如何仅通过提供大小在 Kotlin 中像在 Java 中一样创建数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35253368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:28:01  来源:igfitidea点击:

How can I create an array in Kotlin like in Java by just providing a size?

javaarraysarraylistkotlin

提问by Kevin Mathew

How can I create a Array like we do in java?

我怎样才能像在java中那样创建一个数组?

int A[] = new int[N];

How can I do this in Kotlin?

我怎样才能在 Kotlin 中做到这一点?

采纳答案by hotkey

According to the reference, arrays are created in the following way:

根据参考,数组的创建方式如下:

  • For Java's primitive types there are distinct types IntArray, DoubleArrayetc. which store unboxedvalues.

    They are created with the corresponding constructors and factory functions:

    val arrayOfZeros = IntArray(size) //equivalent in Java: new int[size]
    val numbersFromOne = IntArray(size) { it + 1 }
    val myInts = intArrayOf(1, 1, 2, 3, 5, 8, 13, 21)
    

    The first one is simillar to that in Java, it just creates a primitive array filled with the default value, e.g. zero for Int, falsefor Boolean.

  • Non primitive-arrays are represented by Array<T>class, where Tis the items type.

    Tcan still be one of types primitive in Java (Int, Boolean,...), but the values inside will be boxed equivalently to Java's Integer, Doubleand so on.

    Also, Tcan be both nullable and non-nulllike Stringand String?.

    These are created in a similar way:

    val nulls = arrayOfNulls<String>(size) //equivalent in Java: new String[size]
    val strings = Array(size) { "n = $it" } 
    val myStrings = arrayOf("foo", "bar", "baz")
    
    val boxedInts = arrayOfNulls<Int>(size) //equivalent in Java: new Integer[size]
    val boxedZeros = Array(size) { 0 }
    
  • 对于Java的基本类型有不同的类型IntArrayDoubleArray等等。它存储拆箱值。

    它们是使用相应的构造函数和工厂函数创建的:

    val arrayOfZeros = IntArray(size) //equivalent in Java: new int[size]
    val numbersFromOne = IntArray(size) { it + 1 }
    val myInts = intArrayOf(1, 1, 2, 3, 5, 8, 13, 21)
    

    第一个和 Java 中的类似,它只是创建一个用默认值填充的原始数组,例如零 for Intfalsefor Boolean

  • 非原始数组由Array<T>类表示,其中T是项目类型。

    T仍然可以是 Java 中的原始类型之一(Int, Boolean,...),但是里面的值将被装箱到 Java 的IntegerDouble等等。

    此外,T既可以为也可以为非空,例如StringString?

    它们以类似的方式创建:

    val nulls = arrayOfNulls<String>(size) //equivalent in Java: new String[size]
    val strings = Array(size) { "n = $it" } 
    val myStrings = arrayOf("foo", "bar", "baz")
    
    val boxedInts = arrayOfNulls<Int>(size) //equivalent in Java: new Integer[size]
    val boxedZeros = Array(size) { 0 }
    

回答by Gibolt

In Kotlin, creating an IntArray of size N is simple. Use IntArray(n)or the appropriate type, as detailed thoroughly in hotkey's answer.

在 Kotlin 中,创建一个大小为 N 的 IntArray 很简单。使用IntArray(n)或 适当的类型,如热键的回答中详述。

When utilizing your fixed size array, you can use Kotlin Destructuring

在使用固定大小的数组时,您可以使用Kotlin Destructuring

// Create fixed sized array
val point = IntArray(2)

// Access values directly
val (x, y) = point

In this case, xwill be taken from index 0, yfrom index 1, etc.

在这种情况下,x将取自索引 0、y索引 1 等。