从intarray的阵列列表中检索值
所以我知道我错过了一些明显的东西,但是,在搜索类似/相关的问题后,我不太清楚我做错了什么。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我读了您的代码,我认为问题是您使用的intarray,它是一个对象,每次将其添加到列表中时,它都是同一对象。因此,最后,它始终是相同的元素。
请将代码更改为以下内容:
应该解决您的问题。
这是参考对象的解释
这是关于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:
that should resolve your problem.
Here is the explanation of the reference object
Here is the description about kotlin, it explains by image and content, you can read this.
问题是:
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)})
最终代码看起来像这样:
或更简洁(可能太多):
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:
or even more concise (probably too much):
听起来您需要堆栈。您可以为此使用mutablelist,也可以使用类。使用 size,addlast(element),clear,包含(element),isempty(),last()和removelast(),您可以使用所有内容来操纵像撤消列表之类的东西。
要构建它,您将要做:
输出:
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:
Output: