简体   繁体   English

如何在Golang中创建一个三维数组

[英]How to create a three-dimensional array in Golang

I'm trying to create a three-dimensional array which contains blocks (like a rubiks-cube). 我正在尝试创建一个包含块的三维数组(如rubiks-cube)。

I tried many things but I can't get it to work. 我尝试了很多东西,但我无法让它发挥作用。

func generateTiles(x int, y int, z int) [][][]*tile{
  var tiles [][][]*tile

  // Something here
  // resulting in a x by y by z array
  // filled with *tile

  return tiles
}

Any suggestions? 有什么建议么?

You have to initialize each layer on its own. 您必须自己初始化每个图层。 Example ( on play ): 示例( 正在播放 ):

tiles = make([][][]*tile, x)

for i := range tiles {
    tiles[i] = make([][]*tile, y)
    for j := range tiles[i] {
        tiles[i][j] = make([]*tile, z)
    }
}

I'd personally use a 1D slice for performance reasons, I'm adding this as an alternative: 出于性能原因,我个人会使用一维切片,我将其添加为替代方案:

type Tile struct {
    x, y, z int
}

type Tiles struct {
    t       []*Tile
    w, h, d int
}

func New(w, h, d int) *Tiles {
    return &Tiles{
        t: make([]*Tile, w*h*d),
        w: w,
        h: h,
        d: d,
    }
}

// indexing based on http://stackoverflow.com/a/20266350/145587
func (t *Tiles) At(x, y, z int) *Tile {
    idx := t.h*t.w*z + t.w*y
    return t.t[idx+x]
}

func (t *Tiles) Set(x, y, z int, val *Tile) {
    idx := t.h*t.w*z + t.w*y
    t.t[idx+x] = val
}

func fillTiles(w int, h int, d int) *Tiles {
    tiles := New(w, h, d)

    for x := 0; x < w; x++ {
        for y := 0; y < h; y++ {
            for z := 0; z < d; z++ {
                tiles.Set(x, y, z, &Tile{x, y, z})
            }
        }
    }

    return tiles
}

playground 操场

There is an example for creating a two dimensional array on GoByExample: https://gobyexample.com/arrays . 有一个在GoByExample上创建二维数组的示例: https ://gobyexample.com/arrays。 You should be able to expand that into a three dimensional case. 您应该能够将其扩展为三维案例。

Here is what I came up with. 这就是我想出的。

CODE

package main

import (
    "fmt"
)

type Tile struct {
    value int
}

func create3D( x, y, z int) [][][]*Tile {
    result := make([][][]*Tile,x)
    for i := 0 ; i < x ; i++ {
        result[i] = make([][]*Tile,y);
        for j := 0; j < y; j++ {
            result[i][j] = make([]*Tile,z);
            for k := 0 ; k < z; k++ {
                result[i][j][k] = new(Tile)
                result[i][j][k].value = i + j + k;
            }
        }
    }
    return result
}

func main() {
    X := 3
    Y := 4
    Z := 5

    mat := create3D( X , Y , Z);
    for i := 0; i < X; i++ {
        for j := 0 ; j < Y; j++ {
            for k := 0 ; k < Z; k++ {
                fmt.Printf("%d ",mat[i][j][k].value)
            }
            fmt.Println();
        }
        fmt.Println();
    }

}

It works like this, but to me this feels very inefficient. 它的工作方式是这样的,但对我而言,效率非常低。 Using the append-operation this many times. 多次使用append-operation。 And it feels bloated, this should be possible in a simpler way. 它感觉臃肿,应该可以更简单的方式。

func generateTiles(x int, y int, z int) [][][]*tile {
    var tiles [][][]*tile

    for i := 0; i < z; i++ {
        var layer [][]*tile
        for j := 0; j < y; j++ {
            var row []*tile
            for k := 0; k < x; k++ {
                var t *tile
                t = &tile{}
                row = append(row, t)
                count++
            }
            layer = append(layer, row)
        }
        tiles = append(tiles, layer)
    }

    return tiles
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM