Kotlin 中的二维数组 [英] 2D Array in Kotlin
本文介绍了Kotlin 中的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Kotlin 中制作 2D Int 数组?我正在尝试将此代码转换为 Kotlin:
How do you make a 2D Int array in Kotlin? I'm trying to convert this code to Kotlin:
int[][] states = new int[][] {
new int[]{-android.R.attr.state_pressed}, // not pressed
new int[] { android.R.attr.state_pressed} // pressed
};
int[] colors = new int[] {
foregroundColor,
accentColor,
accentColor
};
ColorStateList myList = new ColorStateList(states, colors);
这是我尝试过的一次尝试,其中第一个二维数组不起作用,但我让一维数组起作用:
Here is one attempt I tried, where the first 2D array didn't work, but I got the 1D array to work:
//This doesn't work:
var states: IntArray = intArrayOf(
intArrayOf(-android.R.attr.state_pressed), // not pressed
intArrayOf(android.R.attr.state_pressed) // pressed
);
//This array works:
var colors: IntArray = intArrayOf(
foregroundColor,
accentColor,
accentColor
);
val myList: ColorStateList = ColorStateList(states, colors);
推荐答案
您正试图将 IntArrays 放入另一个数组中以使其成为二维数组.该数组的类型不能是 intArray,这就是失败的原因.用 arrayOf
而不是 intArrayOf
包裹你的初始数组.
You are trying to put your IntArrays inside another array to make it 2-dimensional.
The type of that array cannot be intArray, which is why this fails.
Wrap your initial arrays with arrayOf
instead of intArrayOf
.
val even: IntArray = intArrayOf(2, 4, 6)
val odd: IntArray = intArrayOf(1, 3, 5)
val lala: Array<IntArray> = arrayOf(even, odd)
这篇关于Kotlin 中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文