c# – 写成名称[x,y]的数组而不是名称[x] [y]

c# – 写成名称[x,y]的数组而不是名称[x] [y],第1张

概述参见英文答案 > What are the differences between a multidimensional array and an array of arrays in C#?                                    9个 这两种语法有什么区别? 第一个是多维数组,第二个是锯齿状数组.您可以查看 this question以获得两者之间差异的描述,但这 参见英文答案 > What are the differences between a multidimensional array and an array of arrays in C#?                                    9个
这两种语法有什么区别?解决方法 第一个是多维数组,第二个是锯齿状数组.您可以查看 this question以获得两者之间差异的描述,但这里有一个重要的信息:

A multIDimensional array creates a nice linear memory layout while a
jagged array implIEs several extra levels of indirection.

Looking up the value jagged[3][6] in a jagged array var jagged = new
int[10][5] works like this: Look up the element at index 3 (which is
an array) and look up the element at index 6 in that array (which is a
value). For each dimension in this case,there’s an additional look up
(this is an expensive memory access pattern).

A multIDimensional array is laID out linearly in memory,the actual
value is found by multiplying together the indexes. However,given the
array var mult = new int[10,30] the Length property of that
multIDimensional array returns the total number of elements i.e. 10 *
30 = 300.

The Rank property of a jagged array is always 1,but a
multIDimensional array can have any rank. The GetLength method of any
array can be used to get the length of each dimension. For the
multIDimensional array in this example mult.GetLength(1) returns 30.

Indexing the multIDimensional array is faster e.g. given the
multIDimensional array in this example mult[1,7] = 30 * 1 + 7 = 37,
get the element at that index 37. This is a better memory access
pattern because only one memory location is involved,which is the
base address of the array.

A multIDimensional array therefore allocates a continuous memory
block,while a jagged array does not have to be square like. e.g.
jagged07001.Length does not have to equal jagged[2].Length which would
be true for any multIDimensional array.

更新:

multi和jagged数组之间的一个主要区别是multi必须始终为“square”,这意味着任何两个索引在其子数组中将具有相同数量的元素.锯齿状阵列没有此要求.看看下面的代码:

var jagged = new int[3][]; //not defining the size of the child array...var multi = new int[3,8]; //defining a 3x8 "square"var multiBad = new int[3,]; //Syntax error!var jaggedSquare= new int[3][8]; //another 3x8 "square"
总结

以上是内存溢出为你收集整理的c# – 写成名称[x,y]的数组而不是名称[x] [y]全部内容,希望文章能够帮你解决c# – 写成名称[x,y]的数组而不是名称[x] [y]所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1222272.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存