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[,]
中表示矩形2D数组.第一种类型也称为锯齿状数组,第二种类型称为多维数组.区别与C#中的相同,因此您可能需要查看多维数组.在 F#Wiki 中也有一个出色的文档.
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屋!