阵列围棋治疗 [英] Treatment of Arrays in Go

查看:221
本文介绍了阵列围棋治疗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看了在 http://golang.org/doc/effective_go.html#arrays以下 ...


  

      
  • 数组是值。指定一个阵列到另一个副本所有
      元素。

  •   
  • 在特定的,如果你传递一个数组给一个函数,它将收到一份副本
      数组,而不是一个指向它的。

  •   

...我希望在以下code,它 ARR2 来从改编分明,和 main()中的 改编是从不同的洗牌()改编。有人请解释为什么下列code洗牌 ARR2 ?我知道围棋仍然是一个年轻的语言;或许阵列的待遇有什么改变?

 主包进口(
        FMT
        兰特
        时间
)FUNC洗牌(ARR [] INT){
        rand.Seed(time.Nanoseconds())
        对于i:= LEN(ARR) - 1; I> 0;一世 - {
                记者:= rand.Intn(I)
                改编[I],编曲[J] = ARR [J],编曲[I]
        }
}FUNC的main(){
        ARR:= [] INT {1,2,3,4,5}
        ARR2:= ARR
        洗牌(ARR)
        为_,I:=范围ARR2 {
                fmt.Printf(%D,我)
        }
}


解决方案

我觉得你的问题是,你混淆阵列和切片。

数组是固定长度值的列表。你实际上没有使用你的榜样任何阵列。数组可以声明几个方面:

  ARR1:= [3] INT {1,2,3} // 3整数数组,1-3
ARR2:= [...] INT {1,2,3} //同为previous线,但我们让
                          //编译器的身影了数组的大小
VAR ARR3 [3] INT //一个归零的3整数数组

您正在使用的切片。切片是一种潜在的数组的引用。有几种方法来分配新切片:

  slice1:= [] INT {1,2,3} //长度为3的含有整数1-3片
slice2:=使([] INT,3)//含有三个零值的整数长度为3的片
slice3:=令([] INT,3,5)//长度为3片,容量5这一切归零

任何其他切片赋值只是复制到一个数组的引用。

现在,我们已经确定了,行

  ARR:= [] {INT 1,2,3,4,5}

创建了一个切片引用包含数字的匿名底层数组1-5。

  ARR2:= ARR

引用重复 - 它的的复制底层的数组。所以有一基本阵列和两个引用它。这就是为什么ARR2变化的内容,当你修改ARR的内容。他们引用同一个数组。

Having read the following at http://golang.org/doc/effective_go.html#arrays...

  • Arrays are values. Assigning one array to another copies all the elements.
  • In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it.

... I expect in the following code that arr2 to be distinct from arr, and main()'s arr to be distinct from shuffle()'s arr. Can someone please explain why the following code shuffles arr2? I know Go is still a young language; perhaps the treatment of arrays has changed?

package main

import (
        "fmt"
        "rand"
        "time"
)

func shuffle(arr []int) {
        rand.Seed(time.Nanoseconds())
        for i := len(arr) - 1; i > 0; i-- {
                j := rand.Intn(i)
                arr[i], arr[j] = arr[j], arr[i]
        }
}

func main() {
        arr := []int{1, 2, 3, 4, 5}
        arr2 := arr
        shuffle(arr)
        for _, i := range arr2 {
                fmt.Printf("%d ", i)
        }
}

解决方案

I think your problem is that you're confusing arrays and slices.

Arrays are fixed-length lists of values. You're actually not using any arrays in your example. Arrays can be declared a few ways:

arr1 := [3]int{1, 2, 3}   // an array of 3 integers, 1-3
arr2 := [...]int{1, 2, 3} // same as the previous line, but we're letting
                          // the compiler figure out the size of the array
var arr3 [3]int           // a zeroed out array of 3 integers

You're using slices. A slice is a reference to an underlying array. There are a few ways to allocate new slices:

slice1 := []int{1, 2, 3}    // a slice of length 3 containing the integers 1-3
slice2 := make([]int, 3)    // a slice of length 3 containing three zero-value integers
slice3 := make([]int, 3, 5) // a slice of length 3, capacity 5 that's all zeroed out

Any other slice assignments are just duplicating a reference to an array.

Now that we've established that, the line

arr := []int{1, 2, 3, 4, 5}

creates a slice referencing an anonymous underlying array that contains the numbers 1-5.

arr2 := arr

duplicates that reference -- it does not copy the underlying array. So there's one underlying array and two references to it. That's why the contents of arr2 change when you modify the contents of arr. They're referencing the same array.

这篇关于阵列围棋治疗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆