Kotlin中的2D阵列

问题描述 投票:20回答:7

你如何在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);
arrays kotlin
7个回答
27
投票

您正在尝试将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)

26
投票

您可以将此行代码用于Integer数组。

val array = Array(row, {IntArray(column)})

这行代码非常简单,就像一维数组一样,也可以像java二维数组一样访问。


2
投票

1.嵌套的arrayOf电话

您可以嵌套arrayOf的调用,例如,创建一个IntArray数组,执行以下操作:

val first: Array<IntArray> = arrayOf(
    intArrayOf(2, 4, 6),
    intArrayOf(1, 3, 5)
)

请注意,IntArray本身只接受Int类型的参数作为参数,所以你不能拥有一个显然没有多大意义的IntArray<IntArray>

2.使用Array::constructor(size: Int, init: (Int) -> T)重复逻辑

如果要根据索引创建具有某些逻辑行为的内部数组,可以使用带有大小和init块的Array构造函数:

val second: Array<IntArray> = Array(3) {
    intArrayOf(it * 1, it * 2, it * 3, it * 4)
} 
//[[0, 0, 0, 0], [1, 2, 3, 4], [2, 4, 6, 8]]

1
投票

看来你正试图在Kotlin创建一个ColorStateList。代码有点乱,我会尽量保持它的可读性:

val resolvedColor = Color.rgb(214,0,0)
val states = arrayOf(
    intArrayOf(-android.R.attr.state_pressed),
    intArrayOf(android.R.attr.state_pressed)
)

val csl = ColorStateList(
    states,
    intArrayOf(resolvedColor, Color.WHITE)
)

1
投票

简答:

// A 6x5 array of Int, all set to 0.
var m = Array(6) {Array(5) {0} }

这是另一个例子,详细介绍了正在发生的事情:

// a 6x5 Int array initialise all to 0
var m = Array(6, {i -> Array(5, {j -> 0})})

第一个参数是大小,第二个lambda方法用于初始化。


1
投票

我在创建矩阵时一直在使用这种单线程

var matrix: Array<IntArray> = Array(height) { IntArray(width) }

0
投票

为此,您可以使用简单的1D(线性)阵列。例如,这是我的类,用于Double值的矩形数组:

/**
 * Rect array of Double values
 */
class DoubleRectArray(private val rows: Int, private val cols: Int) {
    private val innerArray: DoubleArray

    init {
        if(rows < 1) {
            throw IllegalArgumentException("Rows value is invalid. It must be greater than 0")
        }

        if(cols < 1) {
            throw IllegalArgumentException("Cols value is invalid. It must be greater than 0")
        }

        innerArray = DoubleArray(rows*cols)
    }

    /**
     *
     */
    fun get(row: Int, col: Int): Double {
        checkRowAndCol(row, col)
        return innerArray[row*cols + col]
    }

    /**
     *
     */
    fun set(row: Int, col: Int, value: Double) {
        checkRowAndCol(row, col)
        innerArray[row*cols + col] = value
    }

    /**
     *
     */
    private fun checkRowAndCol(row: Int, col: Int) {
        if(row !in 0 until rows) {
            throw ArrayIndexOutOfBoundsException("Row value is invalid. It must be in a 0..${rows-1} interval (inclusive)")
        }

        if(col !in 0 until cols) {
            throw ArrayIndexOutOfBoundsException("Col value is invalid. It must be in a 0..${cols-1} interval (inclusive)")
        }
    }
}

0
投票

您可以创建两个一维数组并将它们添加到新数组中。

val unChecked = intArrayOf(-android.R.attr.state_checked)
val checked = intArrayOf(android.R.attr.state_checked)
val states = arrayOf(unChecked, checked)

val thumbColors = intArrayOf(Color.WHITE, Color.parseColor("#55FFD4"))
val stateList = ColorStateList(states, thumbColors)
© www.soinside.com 2019 - 2024. All rights reserved.