从 IntArray 的 ArrayList 中检索值

Retrieving values from an ArrayList of IntArrays

所以我知道我遗漏了一些明显的东西,但是,在搜索 similar/related 个问题之后,我不太清楚我做错了什么。

Kotlin 的新手,所以可能是我理解不正确。

创建一个 ArrayList,因为我需要一个不断增长的项目列表,从 none 开始。把它想象成一个撤销列表。它会长到一个未知的大小。在某些时候,我会在需要时将其重置为“空”。

在此列表中,我需要一个整数数组。这 3 个值是一个坐标系 - 如果它很重要(即 x、y、z)。

我尽一切努力,最终只能检索添加的最终 IntArray 集。

使用: https://developer.android.com/training/kotlinplayground

fun main() {
    // array list
    var myList = arrayListOf<IntArray>()
    // 3 item "test" array to populate array list with
    var myArr = IntArray(3){0}
    
    // setup Array list with 3 items
    for ( b in 0..2 ) {
        // fake/create a temp array with some simple values
        for ( i in 0..2 ) { myArr[i] = 3+b+(3*i) }
        
        // add it to the List
        myList.add(b, myArr)
        // confirm values
        println ( "Added [" + myList.lastIndex +"] = " + myArr[0] +"-"+ myArr[1] +"-"+ myArr[2] )
    }
    // confirm size of Array List
    println ( "size: " + myList.size )

    // test pull the middle array from the ArrayList
    // indices should be: 0, 1 and 2
    var testArr = myList.get(1)
    println ( "for idx 1: vals: " + testArr[0] +"-"+ testArr[1] +"-"+ testArr[2])
    
    // test display all values for all arrays
    myList.forEach {
        println ( "Vals: " + it[0] +"-"+ it[1] +"-"+ it[2] )
    }
    // another method to do same ?
    for ((index,value) in myList.withIndex()) {
        println("index: $index ... " + value[0] +"-"+ value[1] +"-"+ value[2])
    }
}

输出为:

Added [0] = 3-6-9
Added [1] = 4-7-10
Added [2] = 5-8-11
size: 3
for idx 1: vals: 5-8-11
Vals: 5-8-11
Vals: 5-8-11
Vals: 5-8-11
index: 0 ... 5-8-11
index: 1 ... 5-8-11
index: 2 ... 5-8-11

在重复“5-8-11”之前,一切都说得通......我做错了什么?

我看了你的代码,我认为问题出在你使用的IntArray,它是一个对象,每次你将它添加到列表中时,它都是同一个对象。所以最后,它总是相同的元素。 请将代码更改为以下内容:

...
for ( b in 0..2 ) {
        // fake/create a temp array with some simple values
        var myArr = IntArray(3){0}
        for ( i in 0..2 ) { myArr[i] = 3+b+(3*i) }
        
        // add it to the List
        myList.add(b, myArr)
        // confirm values
        println ( "Added [" + myList.lastIndex +"] = " + myArr[0] +"-"+ myArr[1] +"-"+ myArr[2] )
    }
...

这应该可以解决您的问题。

这里是参考对象的解释

As you work with objects, it's important to understand references. A reference is an address that indicates where an object's variables and methods are stored. You aren't using objects when you assign an object to a variable or pass an object to a method as an argument. You aren't even using copies of the objects. Instead, you're using references to those objects.

这里是description about kotlin,图文并茂,大家可以看看

Creating an ArrayList, as I need a growing list of items, starting with none. Think of it like an undo list. It'll grow to an unknown size. At some point, I'll reset it back to "empty" when needed.

听起来您需要一个 Stack。您可以为此使用 MutableList,或 ArrayDeque class。有了 size、addLast(element)、clear、contains(element)、isEmpty()、last() 和 removeLast(),您便拥有了操作诸如撤消列表之类的东西的一切。

要构建它,您可以这样做:

val stack = ArrayDeque<IntArray>()
for (b in 0..2) {
  val intArray = IntArray(3)
  for (i in 0..2) {
    intArray[i] = 3 + b + (3 * i)
  }
  stack.addLast(intArray)
}

stack.forEach { println(it.joinToString("-")) }

输出:

3-6-9
4-7-10
5-8-11

问题在于:

for ( i in 0..2 ) { myArr[i] = 3+b+(3*i) }

你总是修改和添加同一个对象:myArr .

要修复,替换

for ( i in 0..2 ) { myArr[i] = 3+b+(3*i) }

val a = IntArray(3) { i -> 3+b+(3*i) }

然后添加a:

myList.add(a)

或者,如果填充 IntArray 就像示例中一样简单:

myList.add(IntArray(3) { i -> 3+b+(3*i) })

最终代码如下所示:

fun main() {
    val myList = arrayListOf<IntArray>()

    // setup Array list with 3 items
    for ( b in 0..2 ) {
        myList.add(IntArray(3) { i -> 3+b+(3*i) })
    }
    for ((index,value) in myList.withIndex()) {
        println("index: $index ... " + value[0] +"-"+ value[1] +"-"+ value[2])
    }
}

或更简洁(可能太多了):

fun main() {
    val myList = List(3) { b -> IntArray(3) { i -> 3 + b + (3 * i) } }

    for ((index, value) in myList.withIndex()) {
        println("index: $index ... " + value[0] + "-" + value[1] + "-" + value[2])
    }
}