Array.create and jagged array

大憨熊 提交于 2019-12-18 08:47:27

问题


Can't understand the reason of such behavior:

let example count = 
    let arr = Array.create 2 (Array.zeroCreate count)
    for i in [0..count - 1] do
        arr.[0].[i] <- 1
        arr.[1].[i] <- 2
    arr

example 2 |> Array.iter(printfn "%A")

Print:

[|2; 2|]
[|2; 2|]

https://dotnetfiddle.net/borMmO

If I replace:

let arr = Array.create 2 (Array.zeroCreate count)

to:

let arr = Array.init 2 (fun _ -> Array.zeroCreate count)

Everything will work as expected:

let example count = 
    let arr = Array.init 2 (fun _ -> Array.zeroCreate count)
    for i in [0..count - 1] do
        arr.[0].[i] <- 1
        arr.[1].[i] <- 2
    arr

example 2 |> Array.iter(printfn "%A")

Print:

[|1; 1|]
[|2; 2|]

https://dotnetfiddle.net/uXmlbn

I think the reason is the fact that the array - a reference type. But I want to understand why this is happening. Since I didn't expect such results.


回答1:


When you write:

let arr = Array.create 2 (Array.zeroCreate count)

You are creating an array where each element is a reference to the same array. This means that mutating a value using arr.[0] also mutates the value in arr.[1] - because the two array elements are pointing to the same mutable array. You end up with:

[| x  ; x |]
    \  /
 [| 0; 0 |]

When you write:

let arr = Array.init 2 (fun _ -> Array.zeroCreate count)

The provided function is called for each position in the arr array and so you'll end up with different array for each element (and so arr.[0] <> arr.[1]). You end up with:

     [| x ;  y |]
       /       \
[| 0; 0 |]   [| 0; 0 |]


来源:https://stackoverflow.com/questions/34252606/array-create-and-jagged-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!