如何在Kotlin中制作2D Int数组?我正在尝试将此代码转换为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数组起作用:
//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包裹您的初始数组。

val even: IntArray = intArrayOf(2, 4, 6)
val odd: IntArray = intArrayOf(1, 3, 5)

val lala: Array<IntArray> = arrayOf(even, odd)

08-27 19:29