“真实”的定义是什么?多维数组以及哪些语言支持它们?
我读过的大多数编程书籍都有这样一句话:
“X 语言不支持真正的多维数组,但你可以用数组的数组来模拟(近似)它们。”
由于我的大部分经验都是基于 C 的语言,即 C++、Java、JavaScript、php 等,因此我不确定什么是“真正的”多维数组。
true 多维数组的定义是什么以及哪些语言支持它? 另外,如果可能的话,请在代码中显示一个真正的多维数组的示例。
Most of the programming books I have ever read, have the following line:
"X language does not support true multidimensional arrays, but you can simulate (approximate) them with arrays of arrays."
Since most of my experience has been with C-based languages, i.e. C++, Java, JavaScript, php, etc., I'm not sure of what a "true" multidimensional array is.
What is the definition of a true multidimensional array and what languages support it?
Also, please show an example of a true multidimensional array in code if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C# 支持真正的多维数组和可以替代的“锯齿状”数组(数组的数组)。
锯齿状数组通常被认为更好,因为它们可以完成多维数组可以做的所有事情,甚至更多。在交错数组中,您可以让每个子数组具有不同的大小,而在多维数组中则不能这样做。甚至有一个代码分析规则可以实现此效果(http://msdn.microsoft.com/en-us /library/ms182277.aspx)
C# supports both true multi-dimensional arrays, and "jagged" arrays (array of arrays) which can be a replacement.
Jagged arrays are generally considered better since they can do everything a multi-dimensional array can do and more. In a jagged array you can have each sub-array be a different size, whereas you cannot do that in a multi-dimensional array. There is even a Code Analysis rule to this effect (http://msdn.microsoft.com/en-us/library/ms182277.aspx)
Java 也使用它们
这是我发现的一个有趣的用法
Java uses them too
Here's an interesting use of it that I've found
没有翻阅 Sun 和 Microsoft 网站上的大量文献,这就是我在 C 时代的记忆。希望这有帮助。
为了简单起见,如果我们只考虑二维 - 数组可以表示为二维数组和指针数组。在代码中这相当于
int x[15][20];
整数*y[15];
在此示例中,x[5][6] 和 b[5][6] 在语法上都是有效的,并且最终引用单个 int。
话虽如此,x 是一个真正的二维数组:一旦创建它,就会预留 300 个位置(可以包含 int),并且您可以使用众所周知的下标约定来访问这个矩形(使用15 行 20 列)数组,您可以通过计算 (20 * row) + col 得到 x[row,col]。
然而,在 y 的情况下,在定义结构时,仅分配了 15 个指针,但未初始化。 (初始化需要显式完成)
这种方法有优点和缺点(指针数组或“数组的数组”或所谓的交错数组):
优点:
此数组的行可以具有不同的长度,即每个元素y 不需要指向二十个元素的 ROW;一个元素可能指向 2 个元素,第 2 个元素可能指向 3 个元素,第 3 个元素可能指向 0 个元素,依此类推。
缺点:
然而,在最好的情况下,如果 y 的每个元素都指向 20 个元素的数组,那么将预留 300 个整数位置,另外还有 10 个用于指针的单元格。
从当前示例的角度来看,上面给出的 C Sharp 示例(在之前的一篇文章中)应该足够了。
Without going through the reams of literature on the Sun and Microsoft sites, this is what I remember from my C days. Hope this helps.
To make it simple, if we just think in 2 dimensions - Arrays can either be represented as a two-dimensional array and an array of pointers. In code this amounts to
int x[15][20];
int *y[15];
In this example, x[5][6] and b[5][6] are both valid syntactically and end up referring to a single int.
That being said, x is a true two-dimensional array: Once you create it , there will be 300 locations (that can contain int) that have been set aside, and you can use the well known subscript convention to access this rectangular (with 15 rows and 20 columns) array where you can get to x[row,col] by calculating (20 * row) + col.
However in case of y, while the structure is being defined, only 15 pointers are allocated, but not initialized. (Initialization will need to be done explicitly)
There are advantages and disadvantages of this approach (pointer array or "array of arrays" or jagged array as it is called):
Advantage:
The rows of this array can be of different lengths i.e. each element of y does not need to point to a twenty-element ROW; one element may point to a 2 elements, 2nd element may point to 3 elements, and 3rd to zero elements and so on.
Disadvantage:
However given a best case scenario, if each element of y does point to a twenty-element array, then there will be 300 integer locations set aside, plus ten cells for the pointers which is additional.
From a current example perspective, the C sharp examples given above (in one of the previous posts) should suffice.
Common Lisp 支持这两种类型的数组。
多维数组称为Array,而“一维”数组称为Vector。
Common Lisp supports both types of arrays.
The multidimensional array is called Array, while the "one-dimensional" one is called Vector.