gpt4 book ai didi

arrays - Kotlin 中的二维数组

转载 作者:IT老高 更新时间:2023-10-28 13:28:50 30 4
gpt4 key购买 nike

如何在 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com