Kotlin中的2D阵列 [英] 2D Array in Kotlin
本文介绍了Kotlin中的2D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在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);
这是我尝试的一个尝试,其中第一个2D数组不起作用,但是我让1D数组起作用:
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中的2D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文