英文:
How do I take a set of arrays (strings) and create a multidimensional slice of those arrays in Golang?
问题
我正在尝试实现一个函数,当传入一个字符串数组时,它会创建一个数组切片,并将每个传入的数组添加到其中。它将返回所有传入数组的最终切片。
作为一个测试,我初始化了4个数组,并尝试创建一个能够实现这一功能的函数。然而,它失败得相当惨。以下是我目前的代码。不确定如何继续。
func main() {
array1 := []string{"x", "o", "x", "_", "_"}
array2 := []string{"0", "o", "x", "_", "_"}
array3 := []string{"o", "o", "o", "_", "_"}
array4 := []string{"o", "o", "o", "o", "o"}
FinalSlice(array1)
FinalSlice(array2)
FinalSlice(array3)
fmt.Println(FinalSlice(array4))
}
func FinalSlice(array []string) [][]string {
var slice [][]string
for i, _ := range slice {
slice[i] = array
}
return slice
}
目前的输出是:
[]
英文:
I am trying to implement a function, that when passed an array of strings, it creates a slice of arrays, and adds to it every array that is passed. It would return the final slice of all arrays that was passed.
Just as a test, I initialized 4 arrays and tried to create a function that would do just this. However, it fails quite miserably. This is what I have so far. Not sure how to go about this.
func main() {
array1 := []string{"x", "o", "x", "_", "_"}
array2 := []string{"0", "o", "x", "_", "_"}
array3 := []string{"o", "o", "o", "_", "_"}
array4 := []string{"o", "o", "o", "o", "o"}
FinalSlice(array1)
FinalSlice(array2)
FinalSlice(array3)
fmt.Println(FinalSlice(array4))
}
func FinalSlice(array []string) [][]string {
var slice [][]string
for i, _ := range slice {
slice[i] = array
}
return slice
}
Right now this is the output:
[]
答案1
得分: 2
这是因为你在一个空切片上进行循环,而空切片没有元素。
所以你的循环
for i, _ := range slice {}
实际上不会执行任何迭代(这就是为什么最终得到一个空数组)。
我不太明白你想要实现的确切结果,但如果你想将数组连接成一个多维数组,你可以使用下面的代码:
package main
import "fmt"
func AppendSlice(source [][]string, value []string) [][]string {
return append(source, value)
}
func main() {
array1 := []string{"x", "o", "x", "_", "_"}
array2 := []string{"0", "o", "x", "_", "_"}
array3 := []string{"o", "o", "o", "_", "_"}
array4 := []string{"o", "o", "o", "o", "o"}
var finalArray [][]string
finalArray = AppendSlice(finalArray, array1)
finalArray = AppendSlice(finalArray, array2)
finalArray = AppendSlice(finalArray, array3)
finalArray = AppendSlice(finalArray, array4)
// [[x o x _ _] [0 o x _ _] [o o o _ _] [o o o o o]]
fmt.Println(finalArray)
}
这个代码可以有很多改进的地方,你可以随意尝试这个解决方案:https://go.dev/play/p/vXvqlRZuOUI
英文:
That is because you loop through an empty slice, which has no elements.
So your loop
for i, _ := range slice {}
will actually run 0 iteration (that's why you end up with an empty array).
I don't understand the exact result you want to achieve, but if you want to concat arrays into a multi-dimensional one, you can use the below code:
package main
import "fmt"
func AppendSlice(source [][]string, value []string) [][]string {
return append(source, value)
}
func main() {
array1 := []string{"x", "o", "x", "_", "_"}
array2 := []string{"0", "o", "x", "_", "_"}
array3 := []string{"o", "o", "o", "_", "_"}
array4 := []string{"o", "o", "o", "o", "o"}
var finalArray [][]string
finalArray = AppendSlice(finalArray, array1)
finalArray = AppendSlice(finalArray, array2)
finalArray = AppendSlice(finalArray, array3)
finalArray = AppendSlice(finalArray, array4)
// [[x o x _ _] [0 o x _ _] [o o o _ _] [o o o o o]]
fmt.Println(finalArray)
}
This may be improved in many ways, feel free to play around with this solution: https://go.dev/play/p/vXvqlRZuOUI
答案2
得分: 1
可以这样说:
func bundleSlices(slices ...[]int) [][]int {
return slices
}
然后:
package main
import "fmt"
func main() {
arr1 := []int{1, 2, 3}
arr2 := []int{4, 5, 6}
arr3 := []int{7, 8, 9}
bundled := bundleSlices(arr1, arr2, arr3)
fmt.Println(bundled)
}
得到结果:
[[1 2 3] [4 5 6] [7 8 9]]
英文:
Couldn't one just say something like
func bundleSlices( slices ...[]int) [][]int {
return slices
}
And then:
package main
import "fmt"
func main() {
arr1 := []int{1, 2, 3}
arr2 := []int{4, 5, 6}
arr3 := []int{7, 8, 9}
bundled := bundleSlices( arr1, arr2, arr3 )
fmt.Println(bundled)
}
to get
[[1 2 3] [4 5 6] [7 8 9]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论