从intarray的阵列列表中检索值

发布于 2025-01-17 20:42:16 字数 1844 浏览 2 评论 0原文

所以我知道我错过了一些明显的东西,但是,在搜索类似/相关的问题后,我不太清楚我做错了什么。

Kotlin 新手,所以可能是我没有正确理解的东西。

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

在此列表中,我需要一个整数数组。这 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”..我做错了什么?

So I know I'm missing something obvious, however, after searching similar/related questions, I can't quite figure out what I'm doing wrong.

New to Kotlin, so probably something I'm not understanding properly.

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.

Inside this list, I need an Array of Integers. These 3 values are a co-ordinate system - if it matters (ie x,y,z).

Everything I try, I keep ending up only being able to retrieve the final IntArray set added.

Using:
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])
    }
}

output is:

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

Everything makes perfect sense up until the repeats of "5-8-11" .. what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

韵柒 2025-01-24 20:42:16

我读了您的代码,我认为问题是您使用的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] )
    }
...

应该解决您的问题。

这是参考对象的解释

当您使用对象时,重要的是要了解参考文献。
引用是指示对象的变量和方法存储在何处的地址。
当将对象分配给变量或将对象传递给方法作为参数时,您不会使用对象。您甚至都不使用对象的副本。相反,您正在使用对这些对象的引用。

这是关于kotlin的描述图像和内容,您可以阅读此内容。

I read your code, I think the problem is the IntArray you use, it is an object, every time you add it to the list, it is the same object. so In the end, it is always the same element.
please change the code to the following:

...
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] )
    }
...

that should resolve your problem.

Here is the explanation of the reference object

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.

Here is the description about kotlin, it explains by image and content, you can read this.

oО清风挽发oО 2025-01-24 20:42:16

问题是:

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

您始终修改和添加相同的对象:myarr

要修复,请替换

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

val a = intarray(3){i-&gt ; 3+b+(3*i)}

,然后添加a

mylist.add(a)

或者,如果填充intarray就像在示例仅:

mylist.add(intarray(3){i - &gt; 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])
    }
}

The issue is that in:

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

you always modifying and adding the same object: myArr .

To fix, replace

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

with

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

and then add a:

myList.add(a)

Or, if populating the IntArray is as simple as in the example just:

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

The final code looks like this:

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])
    }
}

or even more concise (probably too much):

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])
    }
}
雪花飘飘的天空 2025-01-24 20:42:16

创建一个ArrayList,因为我需要越来越多的项目列表,开始
没有。将其视为撤消清单。它会成长为未知的
尺寸。在某个时候,我会在需要时将其重置为“空”。

听起来您需要堆栈。您可以为此使用mutablelist,也可以使用类。使用 size,addlast(element),clear,包含(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

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.

This sounds like you need a Stack. You could use a MutableList for this, or the ArrayDeque class. With size, addLast(element), clear, contains(element), isEmpty(), last() , and removeLast() you have everything at hand for manipulating something like an Undo list.

To construct it you would do:

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("-")) }

Output:

3-6-9
4-7-10
5-8-11
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文