英文:
updating one position in a slice of slices results in multiple updates
问题
我正在学习Go语言并使用切片进行操作,但是我遇到了一个问题。
我有一个默认的矩阵,其中包含"."字符,但是当我尝试将位置0,0的元素改为"#"符号时,所有的n,0位置都变成了"#",换句话说,我开始时有这样一个矩阵:
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
使用以下函数修改位置0,0的元素:
func drawSym(table *[][]string) {
(*table)[0][0] = "#"
}
结果变成了:
[# . . . .]
[# . . . .]
[# . . . .]
[# . . . .]
[# . . . .]
而我想要的矩阵应该是这样的:
[# . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
完整的程序代码可以在playground中查看:https://play.golang.org/p/xvkRhmNvdIP
英文:
I learning go and playing with slices but I have a problem
I have a default matrix with ".", but when I try to change the position 0,0 to a "#" symbol all the n,0 position have a "#", in other words, I start with this:
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
Modify the 0,0 position with this function:
func drawSym(table *[][]string) {
(*table)[0][0] = "#"
}
And i get
[# . . . .]
[# . . . .]
[# . . . .]
[# . . . .]
[# . . . .]
When i want a matrix like this
[# . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
The full code of the program is in playground https://play.golang.org/p/xvkRhmNvdIP
答案1
得分: 0
如果矩阵的大小不变,就不需要传递一个指向切片(或切片的切片)的指针。
因此,您可以简化创建/初始化表格的过程,如下所示:
func createTable(size int) (table [][]string) {
table = make([][]string, size)
for i := 0; i < size; i++ {
table[i] = make([]string, size) // 每次迭代创建一个新行
for j := 0; j < size; j++ {
table[i][j] = "."
}
}
return
}
table := createTable(5)
draw
和 print
函数也被简化了,因为它们也不需要切片指针:
https://play.golang.org/p/KmwynzUj0I-
英文:
If the matrix size does not change, there is no need pass in a pointer to your slice (or slice of slices).
As such you can simplify the creation/initialization of your table like so:
func createTable(size int) (table [][]string) {
table = make([][]string, size)
for i := 0; i < size; i++ {
table[i] = make([]string, size) // create a new row per iteration
for j := 0; j < size; j++ {
table[i][j] = "."
}
}
return
}
table := createTable(5)
the draw
and print
functions are simplified too - as they do not need a slice pointer either:
答案2
得分: 0
请将代码修改如下,以便在每次迭代中都创建一个新的行,而不是使用相同的行。
package main
import (
"fmt"
)
func main() {
table := make([][]string, 5)
initTable(table)
printTable(table)
table = drawSym(table)
printTable(table)
}
func drawSym(table [][]string) [][]string {
newTable := make([][]string, len(table))
for i := 0; i < len(table); i++ {
newTable[i] = make([]string, len(table))
copy(newTable[i], table[i])
}
newTable[0][0] = "#"
newTable[1][1] = "%"
return newTable
}
func initTable(table [][]string) {
for i := 0; i < len(table); i++ {
table[i] = make([]string, len(table))
for j := 0; j < len(table); j++ {
table[i][j] = "."
}
}
}
func printTable(table [][]string) {
for i := 0; i < len(table); i++ {
fmt.Println(table[i])
}
}
输出:
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[# . . . .]
[. % . . .]
[. . . . .]
[. . . . .]
[. . . . .]
这样修改后,每次调用drawSym
函数时,都会创建一个新的table
,并将修改后的结果返回。这样可以确保每次迭代都有一个新的行,而不是使用相同的行。
英文:
Modify your code as follows so that you would not get the same value at all the rows,create a new row for each iteration instead of using the same.
package main
import (
"fmt"
)
func main() {
table := make([][]string, 5)
initTable(table)
printTable(table)
drawSym(table)
printTable(table)
}
func drawSym(table [][]string) {
(table)[0][0] = "#"
(table)[1][1] = "%"
}
func initTable(table [][]string) {
for i := 0; i < len(table); i++ {
table[i] = make([]string, len(table))
for j := 0; j < len(table); j++ {
table[i][j] = "."
}
}
}
func printTable(table [][]string) {
for i := 0; i < len(table); i++ {
fmt.Println((table)[i])
}
}
Output:
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[. . . . .]
[# . . . .]
[. % . . .]
[. . . . .]
[. . . . .]
[. . . . .]
答案3
得分: 0
如果您的矩阵是固定大小的,您可以使用数组而不是切片([5][5]string
)。为了防止代码重复和便于维护,您可以为该矩阵创建一个所需长度的类型(type myMatrix [5][5]string
),并在代码中使用该类型(func printTable(table myMatrix)
)。
例如
package main
import (
"fmt"
)
type myMatrix [5][5]string
func main() {
var table myMatrix
for i := 0; i < len(table); i++ {
for j := 0; j < len(table); j++ {
table[i][j] = "."
}
}
table[0][0] = "#"
printTable(table)
}
func printTable(table myMatrix) {
for i := 0; i < len(table); i++ {
fmt.Println(table[i])
}
}
https://play.golang.org/p/Ok0DBGSfJlA
英文:
If your matrix is of fixed size, you could use arrays instead of slices ([5][5]string
). To prevent code duplication and ease the maintenance, you could make a type of the desired length for that matrix (type myMatrix [5][5]string
) and use that type along your code (func printTable(table myMatrix)
).
Such as
package main
import (
"fmt"
)
type myMatrix [5][5]string
func main() {
var table myMatrix
for i := 0; i < len(table); i++ {
for j := 0; j < len(table); j++ {
table[i][j] = "."
}
}
table[0][0] = "#"
printTable(table)
}
func printTable(table myMatrix) {
for i := 0; i < len(table); i++ {
fmt.Println(table[i])
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论