英文:
Set values in 2D-array by index (Kotlin)
问题
我将我的2D数组定义如下:
```kotlin
var 行 = 4
var 列 = 4
val 我的数组 = Array(行) { IntArray(列) }
fun 填充数组(我的数组: Array<IntArray>) {
我的数组.forEachIndexed { i, 数组 ->
数组.forEach {
val 随机数 = (1..4).random()
我的数组[i][it] = 随机数
Log.i(TAG, 随机数.toString())
}
}
}
我在这一行中遇到了两个问题:
我的数组[i][it] = 随机数
第一个:
如何获取2D数组元素的当前索引“i”
第二个:
这个函数不接受随机生成的数字“随机数”。IDE表示需要 IntArray 类型。我理解我生成了一个具有 int 类型的数组,但我想设置一个 int 类型的值,而不是 IntArray,所以我不太明白该怎么做。
如果我直接按索引访问值:
numbers[0][0] = 3
一切正常。但如果我像上面展示的那样进行迭代,我无法为每个索引设置一个值。
<details>
<summary>英文:</summary>
I define my 2D-array as followed
var row = 4
var col = 4
val myArray = Array(row) { IntArray(col) }
fun fillArray(myArray: Array<IntArray>) {
myArray.forEach {
it.forEach {
var random = (1..4).random()
myArray.set(i, random)
Log.i(TAG, random.toString())
}
}
}
I have two Problems in the line
myArray.set(i, random)
**First:**
how can i get the current index "i" of the 2D-Array Element
**Second:**
the function does not take the random generated number "random". The IDE says the type IntArray is needed.
I understand that i generated an array with int types, but i want to set a value of the type int and not IntArray so i dont quite understand what i shoul do.
If i access the value directly by index this way
numbers[0][0] = 3
everything is fine. But if i iterate over it as i showed above i cant set a value for each index.
</details>
# 答案1
**得分**: 1
只需使用 `numbers[0][0] = 3` 语法,循环应遍历 `myArray` 的 `indices` 以及每个内部数组的 `indices`。
```kotlin
for (outerIndex in myArray.indices) {
for (innerIndex in myArray[outerIndex].indices) {
myArray[outerIndex][innerIndex] = (1..4).random()
}
}
或者如果您喜欢使用 forEach
:
myArray.indices.forEach { outerIndex ->
myArray[outerIndex].indices.forEach { innerIndex ->
myArray[outerIndex][innerIndex] = (1..4).random()
}
}
请注意,不应该在数组上使用 forEach
。这将遍历数组的 元素,但数组的原始元素对您没有用,因为这个过程的整个目的是用随机值填充数组。
实际上,您还可以像这样使用随机值初始化数组:
val myArray = Array(row) {
IntArray(col) { (1..4).random() }
}
英文:
Since you already know the numbers[0][0] = 3
syntax, just use that. The loops should go through the indices
of myArray
and the indices
of each inner array.
for (outerIndex in myArray.indices) {
for (innerIndex in myArray[outerIndex].indices) {
myArray[outerIndex][innerIndex] = (1..4).random()
}
}
Or if you prefer forEach
:
myArray.indices.forEach { outerIndex ->
myArray[outerIndex].indices.forEach { innerIndex ->
myArray[outerIndex][innerIndex] = (1..4).random()
}
}
Note that you should not forEach
over the array. That goes over the elements of the array, but the original elements of the array is of no use to you, since the whole point of this is to fill the array with random values.
In fact, you could also have initialised the array with random values like this:
val myArray = Array(row) {
IntArray(col) { (1..4).random() }
}
答案2
得分: 1
myArray.set(i, random)
这个调用之所以无法工作,是因为有一个非常明显的原因。myArray
的类型是 Array<IntArray>
(类似于 Array<Array<Int>>
)。因此,当期望是 IntArray
时,不能设置一个 Integer
。
要修复它(我在使用命名参数以使其更清晰), 你需要使用 forEachIndexed
:
fun fillArray(myArray: Array<IntArray>) {
myArray.forEach { outerArray ->
outerArray.forEachIndexed { innerIndex, _ ->
val random = (1..4).random()
outerArray.set(innerIndex, random) // 应该使用不同的语法("应该用索引替换")
outerArray[innerIndex] = random // 这是正确的语法
}
}
}
英文:
myArray.set(i, random)
This call doesn't work from quite an obvious reason. The type of myArray
is Array<IntArray>
(so it's kind of Array<Array<Int>>
). So, you cannot set an Integer
when IntArray
is expected.
To fix it (I am using named parameters to make it clearer) you need to use forEachIndexed
:
fun fillArray(myArray: Array<IntArray>) {
myArray.forEach { outerArray ->
outerArray.forEachIndexed { innerIndex, _ ->
val random = (1..4).random()
outerArray.set(innerIndex, random) // we should use different syntax ("Should be replaced with indexing")
outerArray[innerIndex] = random // this is proper syntax
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论