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?
CodePudding user response:
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.
CodePudding user response:
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])
}
}
CodePudding user response:
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