我初始化了INT的二维数组,如下所示
var array = Array(n) { Array(n) { 0 } }
然后我在array
上做了一些逻辑运算来填充值.
when it comes to converting Row values of array
(so Array<Int>
type) to IntArray
type using .toIntArray()
, it behaves differently depending on how to execute corresponding function calls.(toIntArray()
and contentToString()
)
Please look at this screenshot that I ran on XXX.kts scratch file on Android Studio.
如果为test2
,则值返回到0.test
使用我前面提到的执行的逻辑来保持填充值.
我搞不懂这里到底是怎么回事.
我会感激的
- 如果您能解释为什么调用相同的函数,但调用它们的方式不同会导致不同的输出.
- 如果您能建议一种将
Array<Int>
转换为IntArray
的方法(我猜可能是二维数组)
[UPDATE]
Sorry that I didn't attach logic part of code.
The code is like this.(I changed a logic part but it is almost identical. Logic is basically assigning integers in the array.)
for (i in 0..5) {
for (j in 0..5) {
array[i][j] = i + j
}
}
val test = array[0]
println(test.contentToString())
val test2 = test.toIntArray()
println(test.toIntArray().contentToString())
println(test2.contentToString())
and the screenshot with the code look like this below.
[UPDATE 2]
just to be clear for running the code, make sure to initialize n
to be specific number at the very beginning before initializing array
. so it should be something like this.
val n = 6
var array = Array(n) { Array(n) { 0 } }