Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: range and len of multidimensional array?

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 0 1 2 3 ?

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, "\t")
            }
            println()
        }
        println()
    }

}
like image 460
kilves76 Avatar asked Nov 19 '12 08:11

kilves76


People also ask

How do you find the length of an array in Golang?

To get length of an array in Go, use builtin len() function. Call len() function and pass the array as argument. The function returns an integer representing the length of the array.

How do you find the length of a 2D array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.

How do I create a 2 dimensional array in Golang?

To create a 2D array we must specify each dimension. We can then assign individual elements within the array. Here we create a 2 by 2 array of strings. Tip Arrays in Go have fixed sizes.

Can a multidimensional array have more than 3 dimensions?

Arrays can have more than one dimension. For example, the following declaration creates a two-dimensional array of four rows and two columns. int[,] array = new int[4, 2]; The following declaration creates an array of three dimensions, 4, 2, and 3.


1 Answers

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 "fmt"
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, " ")
    }
    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
    }
like image 130
Denys Séguret Avatar answered Nov 11 '22 06:11

Denys Séguret