如何在 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);
最佳答案
您可以将这行代码用于整数数组。
val array = Array(row) { IntArray(column) }
这行代码非常简单,像一维数组一样工作,也可以像java 2D数组一样访问。
关于arrays - Kotlin 中的二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34145495/