2DimentionnalArray Out of Range with Go

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

2DimentionnalArray Out of Range with Go

问题

我尝试构建一个二维数组。我的代码编译通过,但在执行时出现以下错误:

创建高度为10,宽度为10的矩阵
区域长度 = 10,区域容量 = 10
循环 0
循环 1
循环 2
循环 3
循环 4
循环 5
循环 6
循环 7
循环 8
循环 9
panic: 运行时错误:索引超出范围

这是我的代码,Create 方法创建了一个类似于 Go 文档中所示的数组的数组:

// matrice 项目 matrice.go
package matrice

import (
    "fmt"
)

type TwoDimensionnalMatrice struct {
    col, row int
    area     [][]int
}

func (m TwoDimensionnalMatrice) Create(x, y int) {
    m.col = x
    m.row = y
    fmt.Printf("创建高度为 %d,宽度为 %d 的矩阵\n", x, y)
    m.area = make([][]int, m.col)
    fmt.Printf("区域长度 = %d,区域容量 = %d\n", len(m.area), cap(m.area))
    for i := range m.area {
        fmt.Printf("循环 %d\n", i)
        m.area[i] = make([]int, m.row, m.row)
    }
}

func (m TwoDimensionnalMatrice) Get(row, col int) int {
    return m.area[row][col]
}

调用它的代码如下:

package main

import (
    "fmt"
    "matrice"
)

func main() {
    mat := matrice.TwoDimensionnalMatrice{}
    mat.Create(10, 10)
    fmt.Printf("%d", mat.Get(5, 5))
}
英文:

i try to build a two dimensionnal array. My code compile but fail on execution with this error :

Create Matrice with heigth of 10 and width of 10
Area length  = 10, Area cap = 10
Loop 0
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8
Loop 9
panic: runtime error: index out of range

This is my code, The method create, build a array of array like the go doc show. :

// matrice project matrice.go
package matrice

import (
    "fmt"
)

type TwoDimensionnalMatrice struct {
    col, row int
    area     [][]int
}

func (m TwoDimensionnalMatrice) Create(x, y int) {
    m.col = x
    m.row = y
    fmt.Printf("Create Matrice with heigth of %d and width of %d\n", x, y)
    m.area = make([][]int, m.col)
    fmt.Printf("Area length  = %d, Area cap = %d\n", len(m.area), cap(m.area))
    for i := range m.area {
	    fmt.Printf("Loop %d\n", i)
	    m.area[i] = make([]int, m.row, m.row)
    }
}

func (m TwoDimensionnalMatrice) Get(row, col int) int {
    return m.area[row][col]
}

and the code that call it :

package main

import (
    "fmt"
    "matrice"
)

func main() {
    mat := matrice.TwoDimensionnalMatrice{}
    mat.Create(10, 10)
    fmt.Printf("%d", mat.Get(5, 5))
}

答案1

得分: 3

Create()方法的接收者是一个值,而不是一个指针,这意味着你在主函数中修改的是mat的副本,而不是mat本身。

修改为:

func (m *TwoDimensionnalMatrice) Create(x, y int)

或者更可能的是,你想要一个在matrice中返回一个新矩阵的函数,而不是初始化一个现有的矩阵:

func New(x, y int) (*TwoDimensionnalMatrice, error) {
   ... 创建一个矩阵初始化它然后返回它
}
英文:

The receiver for Create() is a value instead of a pointer which means you're mutating a copy of mat in your main function rather than mat itself.

Change:

func (m TwoDimensionnalMatrice) Create(x, y int)

to:

func (m *TwoDimensionnalMatrice) Create(x, y int)

Or more likely, you want a function in matrice that returns a new matrix rather than initialising an existing one:

func New(x, y int) (*TwoDimensionnalMatrice, error) {
   ... create a matrix, initialize it, then return it.
}

huangapple
  • 本文由 发表于 2014年8月2日 21:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/25095633.html
匿名

发表评论

匿名网友

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

确定