Go:数组的数组,数组的切片,切片的数组和切片的切片

huangapple go评论87阅读模式
英文:

Go: Arrays of arrays, arrays of slices, slices of arrays and slices of slices

问题

尝试自学,发现很难找到例子,我的大脑已经混乱了。对于第3和第4个问题非常不确定,需要帮助使第5个问题正常工作。

package main
import "fmt"

func main () {
    println("0. 数组:")
    var a = [...]int{4,5,6,7,8,9} //赋值
    fmt.Println(a,"\n")

    println("1. 切片:")
    var as []int
    as = a[:] //赋值
    fmt.Println(as,"\n")
    
    println("2. 数组的数组:")
    var b [4][len(a)]int
    for i:= range b { //赋值
        b[i]=a
    }
    fmt.Println(b,"\n")

    println("3. 数组的切片:")
    var d [len(b)][]int
    for i:= range b { //赋值
        d[i] = b[i][:] //这样真的有效吗?
    }
    fmt.Println(d,"\n")

    println("4. 切片的数组:")
    var c [][len(a)]int
    c = b[:][:] //赋值,这样真的有效吗?
    fmt.Println(c,"\n")

    println("5. 切片的切片:")
    var e [][]int
//  e = c //  ???
    fmt.Println(e,"\n")
}
英文:

Trying to teach myself and finding it hard to find examples, and my brain's in a knot already. Very unsure about 3 and 4 and need help for making 5 work.

package main
import "fmt"

func main () {
	println("0. Array:")
	var a = [...]int{4,5,6,7,8,9} //assign
	fmt.Println(a,"\n")

	println("1. Slice:")
	var as []int
	as = a[:] //assign
	fmt.Println(as,"\n")
	
	println("2. Array of arrays:")
	var b [4][len(a)]int
	for i:= range b { //assign
		b[i]=a
	}
	fmt.Println(b,"\n")

	println("3. Array of slices:")
	var d [len(b)][]int
	for i:= range b { // assign
		d[i] = b[i][:] //does this really work?
	}
	fmt.Println(d,"\n")

	println("4. Slice of arrays:")
	var c [][len(a)]int
	c = b[:][:] // assign, does this really work?
	fmt.Println(c,"\n")

	println("5. Slice of slices:")
	var e [][]int
//	e = c //  ???
	fmt.Println(e,"\n")
}

答案1

得分: 6

Part 3 works.

Part 4 contains an unnecessary [:].

println("4. 数组的切片:")
var c [][len(a)]int
c = b[:]	// 只需要一个[:],不需要两个
fmt.Println(c, "\n")

b[:] 被解释为一个切片,其中每个元素都是一个 [len(a)]int。如果你再加一个 [:],你实际上是对切片再次进行切片。由于对于任何切片 s,s[:] == s,这是一个无操作。

Part 5, you can slice your array of slices.

println("5. 切片的切片:")
var e [][]int
e = d[:]
fmt.Println(e, "\n")

我在 http://play.golang.org/p/WDvJXFiAFe 上发布了一个完整的示例。

英文:

Part 3 works.

Part 4 contains an unnecessary [:].

println("4. Slice of arrays:")
var c [][len(a)]int
c = b[:]	// one [:], not two
fmt.Println(c, "\n")

b[:] is evaluated as a slice where each element is a [len(a)]int. If you add another [:], you are slicing the slice again. Since for any slice s, s[:] == s, it is a no op.

Part 5, you can slice your array of slices.

println("5. Slice of slices:")
var e [][]int
e = d[:]
fmt.Println(e, "\n")

I posted a complete example at http://play.golang.org/p/WDvJXFiAFe.

答案2

得分: 1

答案取决于你的期望。这个例子展示了切片有一个底层数组,并且你的问题中的例子使用了相同的底层数组。如果你希望切片有独立的内容,你必须创建新的切片并复制数据。(或者使用append的技巧...)

另外,作为一个附注,println将数据发送到stderr而不是stdout,并且对某些数据类型的格式化与fmt.Println不同。为了避免混淆,最好养成使用fmt.Println的习惯。

英文:

The answer to "does this really work?" depends on what you are expecting. Consider this example at http://play.golang.org/p/7Z5hKioTI_

package main

import "fmt"

func main() {
	fmt.Println("0. Array:")
	var a = [...]int{4, 5, 6, 7, 8, 9}	//assign
	fmt.Println(a, "\n")

	fmt.Println("1. Slice:")
	var as []int
	as = a[:]	//assign
	fmt.Println(as, "\n")
	
	fmt.Println("new slice:")
	ns := make([]int, len(a))
	copy(ns, a[:])
	fmt.Print(ns, "\n\n")

	fmt.Println("modifying array...")
	a[0] = 10
	fmt.Print("array is now:\n", a, "\n\n")
	fmt.Print("slice is now:\n", as, "\n\n")
	fmt.Print("new slice is still:\n", ns, "\n")
}

It shows how slices have an underlying array, and that the examples in your OP make slices using the same underlying array. If you want slices to have independent contents, you must make new slices and copy the data. (or there are tricks with append...)

Also as a side note, println sends data to stderr not stdout, and formats some data types differently than fmt.Println. To avoid confusion, it's best to stay in the habit of using fmt.Println.

huangapple
  • 本文由 发表于 2012年11月22日 02:40:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/13499923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定