如何在 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数组一样访问。
https://stackoverflow.com/questions/34145495/
相关文章:
kotlin - 如何从 Kotlin 中的字符串创建枚举?
android - 将支持库更新到 27.0.0 后,我的 fragment 中出现多个错误
kotlin - 如何在 Kotlin 中运行编译的类文件?
android - Kotlin 和惯用的编写方式, 'if not null, else...'
generics - 如何在 Kotlin 中获取具体泛型参数的实际类型参数?
rx-java - Kotlin 和 RxJava - 为什么我的 Single.zip() 没有编
kotlin - 如何在 Kotlin 中引用外部类的实例?