F# 多维数组类型 [英] F# Multidimensional Array Types
问题描述
'a[,,]
和 'a[][][]
有什么区别?它们都表示 3 维数组.它让我写 array3d.[x].[y].[z]
而不是 array3d.[x, y, z]
.
What's the difference between 'a[,,]
and 'a[][][]
? They both represent 3-d arrays.
It makes me write array3d.[x].[y].[z]
instead of array3d.[x, y, z]
.
为什么我不能执行以下操作?
Why I can't do the following?
> let array2d : int[,] = Array2D.zeroCreate 10 10;;
> let array1d = array2d.[0];;
error FS0001: This expression was expected to have type
'a []
but here has type
int [,]
推荐答案
区别在于 'a[][]
表示数组的数组(长度可能不同),而在 'a[,]
,表示一个矩形二维数组.第一种类型也称为锯齿数组,第二种类型称为多维数组.区别与 C# 中的相同,因此您可能需要查看 锯齿状数组 和 多维数组.F# WikiBook 中还有一个优秀的文档.
The difference is that 'a[][]
represents an array of arrays (of possibly different lengths), while in 'a[,]
, represents a rectangular 2D array. The first type is also called jagged arrays and the second type is called multidimensional arrays. The difference is the same as in C#, so you may want to look at the C# documentation for jagged arrays and multidimensional arrays. There is also an excelent documentation in the F# WikiBook.
为了使用图片演示这一点,'a[][]
类型的值可以如下所示:
To demonstrate this using a picture, a value of type 'a[][]
can look like this:
0 1 2 3 4
5 6
7 8 9 0 1
虽然 a[,]
类型的值将始终是一个矩形,并且可能如下所示:
While a value of type a[,]
will always be a rectangle and may look for example like this:
0 1 2 3
4 5 6 7
8 9 0 1
<小时>
要获得多维数组的单个行",您可以使用切片符号:
let row = array2d.[0,*];;
这篇关于F# 多维数组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!