如何用Kotlin创建一个数组,就像在Java中那样,只需提供一个大小?

时间:2023-01-01 21:28:07

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

如何像java那样创建数组呢?

int A[] = new int[N];

How can I do this in Kotlin?

在科特林我怎么做?

1 个解决方案

#1


56  

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

参照文献,数组的创建方式如下:

  • For Java's primitive types there are distinct types IntArray, DoubleArray etc. which store unboxed values.

    对于Java的原始类型,有不同的类型IntArray、DoubleArray等,它们存储未装箱的值。

    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, false for Boolean.

    第一个与Java中的相似,它只创建一个原始数组,其中填充了默认值,例如Int为0,Boolean为false。

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

    非原始数组由数组 类表示,其中T是项类型。

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

    T仍然可以是Java中的一种类型原语(Int, Boolean,…),但是其中的值将被与Java的整数、双精度等等进行等效的装箱。

    Also, T can be both nullable and non-null like String and String?.

    另外,T可以是空的,也可以是非空的,比如String和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 }
    

#1


56  

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

参照文献,数组的创建方式如下:

  • For Java's primitive types there are distinct types IntArray, DoubleArray etc. which store unboxed values.

    对于Java的原始类型,有不同的类型IntArray、DoubleArray等,它们存储未装箱的值。

    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, false for Boolean.

    第一个与Java中的相似,它只创建一个原始数组,其中填充了默认值,例如Int为0,Boolean为false。

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

    非原始数组由数组 类表示,其中T是项类型。

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

    T仍然可以是Java中的一种类型原语(Int, Boolean,…),但是其中的值将被与Java的整数、双精度等等进行等效的装箱。

    Also, T can be both nullable and non-null like String and String?.

    另外,T可以是空的,也可以是非空的,比如String和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 }