什么是数组和多维数组的数组之间的区别? [英] What's the difference between arrays of arrays and multidimensional arrays?

查看:148
本文介绍了什么是数组和多维数组的数组之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个语言无关的讨论,有人在C ++聊天他说,数组和多维数组的数组是两回事。

I had a language-agnostic discussion with someone in the C++ chat and he said that arrays of arrays and multidimensional arrays are two things.

但是,从我了解到,多维数组只不过是其他阵列,所有具有相同大小的数组多。尤其是他说的。

But from what I learned, a multidimensional array is nothing more than an array of other arrays that all have the same size. In particular he is saying

好了,他们那种在C,其中您可以模拟多个维度与嵌套的数组
  但这只是因为C实际上并不支持多维数组

Well, they kind of are in C, where you simulate multiple dimensions with nested arrays but that’s only because C doesn’t actually support multiple dimension arrays

有人能解释什么的多维数组规范计算机科学的定义是,为什么C(或抽象的定义数组的数组)不符合该定义的?

Can someone please explain what the canonical computer-science definition of "multiple dimension arrays" is and why C (or the abstract definition "array of arrays") does not fit that definition?

推荐答案

以这说明.NET数组这很好:

Take .NET arrays which illustrate this nicely:

C#有 交错数组 被定义的区别在嵌套方式:

C# has a distinction between jagged arrays which are defined in a nested fashion:

int[][] jagged = new int[3][];

每个嵌套阵列可以具有不同的长度:

Each nested array can have a different length:

jagged[0] = new int[3];
jagged[1] = new int[4];

(请注意,嵌套阵列中的一个完全不初始化,即

相反, 多维数组 定义如下:

By contrast, a multidimensional array is defined as follows:

int[,] multidim = new int[3, 4];

下面,它没有意义谈论嵌套数组的,确实试图访问 multidim [0] 将是一个编译时错误 - 你需要访问它提供的所有维度,即 multidim [0,1]

Here, it doesn’t make sense to talk of nested arrays, and indeed trying to access multidim[0] would be a compile-time error – you need to access it providing all dimensions, i.e. multidim[0, 1].

它们的类型也不同,如上面的声明显示出来。

Their types are different too, as the declarations above reveal.

此外,它们的处理是完全不同的。例如,你可以在上面交错数组上迭代与类型的对象 INT []

Furthermore, their handling is totally different. For instance, you can iterate over the above jagged array with an object of type int[]:

foreach (int[] x in jagged) …

但迭代多维数组与类型的项目做了 INT

foreach (int x in multidim) …

概念,交错数组是的数组的数组的(中...阵列......循环往复的阵列)的 T 的同时,多维数组的 T 的与一组访问模式的数组(即指数是一个元组)。

Conceptually, a jagged array is an array of arrays (… of arrays of arrays … ad infinitum) of T while a multidimensional array is an array of T with a set access pattern (i.e. the index is a tuple).

这篇关于什么是数组和多维数组的数组之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆