Go:多维数组的范围和长度?

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

Go: range and len of multidimensional array?

问题

在多维数组上使用range和len是可能的吗?

无论是使用var a [3]int8还是

package main

func main() {
var a [3][5]int8

for h := range a {
    println(h)
}
println(len(a))

}

两者都会产生以下输出:
<code>
0
1
2
3
</code>

感谢dystroy的答案,这是一个我能够适应的写入和读取三维数组的示例(在这里发布是因为我在找到任何此类示例时遇到了很多麻烦,所以也许这会帮助其他人):

package main
func main() {
var a [3][5][7]uint8

//将值写入数组
for x, b := range a {
    for y, c := range b {
        for z, _ := range c {
            a[x][y][z] = uint8(x*100+y*10+z)
        }
    }   
}

//从数组中读取值
for _, h := range a {
    for _, i := range h {
        for _, j := range i {
            print(j, "\t")
        }
        println()
    }
    println()
}

}

英文:

Is it possible to use range and len on a multidimensional array?

Either with var a [3]int8 or

package main

func main () {    
        var a [3][5]int8

        for h := range a {
                println(h)
        }
        println(len(a))
}

Both produce
<code>
0
1
2
3
</code>
?

Thanks to dystroy's answer, here's an example of writing and reading a 3-dimensional array i was able to adapt (posting here because I had much trouble finding any examples of this, so maybe this will help someone else):

package main
func main() {
	var a [3][5][7]uint8
	
	//write values to array
	for x, b := range a {
		for y, c := range b {
			for z, _ := range c {
	        		a[x][y][z] = uint8(x*100+y*10+z)
	    		}
		}	
	}
	
	//read values from array
	for _, h := range a {
		for _, i := range h {
			for _, j := range i {
				print(j, &quot;\t&quot;)
			}
			println()
		}
		println()
	}

}

答案1

得分: 17

在Go语言中,和大多数其他语言一样,多维数组被称为数组的数组。len操作符只能给出“外部”数组的长度。

如果你将其视为以下形式,也许对你来说var声明会更清晰:

var a [3]([5]int8)

这也是可以编译的。它是一个大小为3的数组,其元素是大小为5的int8数组。

package main
import "fmt"
func main() {
    var a [3][5]int8
    for _, h := range a {
	    fmt.Println(len(h)) // 每个都打印5
    }
    fmt.Println(len(a))  // 打印3,外部数组的长度
}

输出结果为:

5
5
5
3

要安全地遍历整个矩阵,可以这样做:

for _, h := range a {
	for _, cell := range h {
		fmt.Print(cell, " ")
	}
	fmt.Println()
}

如果需要更改内容,可以这样做:

    for i, cell := range h { // i是索引,cell是值
		h[i] = 2 * cell
	}
英文:

In Go as in most languages, what you call a multidimensional array is an array of arrays. The len operator only gives you the length of the "external" array.

Maybe the var declaration could be clearer for you if you see it as

var a [3]([5]int8)

which also compiles. It's an array of size 3 whose elements are arrays of size 5 of int8.

package main
import &quot;fmt&quot;
func main() {
    var a [3][5]int8
    for _, h := range a {
	    fmt.Println(len(h)) // each one prints 5
    }
    fmt.Println(len(a))  // prints 3, the length of the external array
}

outputs

5
5
5
3

To loop safely through the whole matrix, you can do this :

for _, h := range a {
	for _, cell := range h {
		fmt.Print(cell, &quot; &quot;)
	}
	fmt.Println()
}

If you need to change the content, you may do

    for i, cell := range h { // i is the index, cell the value
		h[i] = 2 * cell
	}

答案2

得分: 2

使用range的解决方案已经提供,所以我将讨论如何在golang中使用长度(len)遍历多维数组。

如果你有一个数组arr[][]

[1,2,3] [4,5,6]

现在len(arr)将输出= 2。

len(arr[1])将输出= 3。

示例代码在这里提供:https://play.golang.org/p/XerzPhkQrhU

英文:

The solution with range is already provided so i'll talk about how to use length (len) to go through a multi-dimesional array in golang.

So if u have an array arr[][] :

[1,2,3]
[4,5,6]

Now the len(arr) will output = 2.

while len(arr[1]) will output = 3.

Sample code is provided here : https://play.golang.org/p/XerzPhkQrhU

答案3

得分: 0

不,第一个产生0,1,2(在范围内的索引)

http://play.golang.org/p/0KrzTRWzKO

而第二个产生3(数组的长度)。

http://play.golang.org/p/0esKFqQZL0

在这两种情况下,您都在使用最外层的数组。

英文:

No, the first one produces 0,1,2 ( index of in the range )

http://play.golang.org/p/0KrzTRWzKO

And the second one produces 3 ( the length of the array ).

http://play.golang.org/p/0esKFqQZL0

In both cases you're using the outermost array.

huangapple
  • 本文由 发表于 2012年11月19日 16:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/13449773.html
匿名

发表评论

匿名网友

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

确定