如何仅通过提供大小在 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
How can I create an array in Kotlin like in Java by just providing a size?
提问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
,DoubleArray
etc. 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
,false
forBoolean
.Non primitive-arrays are represented by
Array<T>
class, whereT
is the items type.T
can still be one of types primitive in Java (Int
,Boolean
,...), but the values inside will be boxed equivalently to Java'sInteger
,Double
and so on.Also,
T
can be both nullable and non-nulllikeString
andString?
.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的基本类型有不同的类型
IntArray
,DoubleArray
等等。它存储拆箱值。它们是使用相应的构造函数和工厂函数创建的:
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
Int
、false
forBoolean
。非原始数组由
Array<T>
类表示,其中T
是项目类型。T
仍然可以是 Java 中的原始类型之一(Int
,Boolean
,...),但是里面的值将被装箱到 Java 的Integer
,Double
等等。此外,
T
既可以为空也可以为非空,例如String
和String?
。它们以类似的方式创建:
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, x
will be taken from index 0, y
from index 1, etc.
在这种情况下,x
将取自索引 0、y
索引 1 等。