像[1] [1] [1] [1] ... [1]在C#最大数组维 [英] Maximum array dimension like a[1][1][1][1]....[1] in C#

查看:234
本文介绍了像[1] [1] [1] [1] ... [1]在C#最大数组维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到<一href="http://stackoverflow.com/questions/22713069/how-many-array-dimensions-are-supported-in-java-like-a1111-1/22713127#22713127">this问题INFACT试图回答它。不过,在回答我想这可能是在情况C#的答案吗?我在找一些MSDN文档或像的Java 任何相关来源

I just saw this question and infact tried to answer it as well. But while answering I thought what could be the answer in case of C#? I am looking for some MSDN doc or any relevant source like in Java

什么是允许在C#中的数组像最大尺寸的[1] [1] [1] [1] ... [1] 。我试图寻找对SO,但未能找到。

What is the maximum dimension allowed for an array in C# like a[1][1][1][1]....[1]. I tried to search on SO but was not able to find.

我起身到最好的是数组理论上最多有 2,147,483,647 元素,因为它使用了一个int进行索引。

The best I got up to was that "An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing."

另外,我知道一个事实,即最大尺寸的阵列可以容纳为

Also I am aware of the fact that Maximum Size that an Array can hold is

System.Int32.MaxValue

请不要让我知道,如果这是一个重复的,我会删除我的问题。

Kindly do let me know if this is a duplicate I will delete my question.

推荐答案

限制为32。我找到了答案这里和(使用谷歌)与code检查的话:

The limit is 32. I found the answer here, and (using google) checked it with the code:

var b = Array.CreateInstance(typeof (int), new int[32]);
var a = Array.CreateInstance(typeof (int), new int[33]);

有同时适用于64和32位目标应用

It applies for both 64 and 32-bit targeted applications.

其实,我不需要用Google搜索了。直接从 MSDN

Actually, I needn't have googled it. Straight from MSDN:

元素是在数组中的值。阵列的长度是元件它可以包含的总数。数组中的排名是在阵列的维数。下界数组的一个维度是数组的维度的起始索引;多维数组可以有不同的边界为每个维度。 阵列最多可以有32尺寸

An element is a value in an Array. The length of an Array is the total number of elements it can contain. The rank of an Array is the number of dimensions in the Array. The lower bound of a dimension of an Array is the starting index of that dimension of the Array; a multidimensional Array can have different bounds for each dimension. An array can have a maximum of 32 dimensions.

不过,我只注意到你在编辑使用的语法(以及语法彼得在下面的评论中使用)是多维数组不如何在C#中定义。多维数组的定义如下:

About jagged arrays

However, I just noticed that the syntax you used in your edit (and also the syntax Peter used in the comments below) isn't how multi-dimensional arrays are defined in C#. Multi-dimensional arrays are defined as:

int[,,,] arr = new int[0,0,0];

和他们的正规整数数组,这正好有多个维度。语法:

And they are arrays of regular integers, that happen to have multiple dimensions. The syntax:

int[][][] = new int[0][][];

定义的东西,在C#中被称为交错数组。铁血阵列只是普通的,一维数组碰巧有其他数组作为元素。你可以有多重数组的多维数组或其他方式,如MSDN文章指出。

Defines something that in C# is called a jagged array. Jagged arrays are just regular, single-dimensional arrays that happen to have other arrays as elements. You can have multi-dimensional arrays of jagged arrays, or the other way around, as the MSDN article points out.

然而它们区别对待,。锯齿状数组是(effectiely)定期*对象只是阵列,正因为如此,不能以相同的方式作为多维数组调节。没有理论极限尺寸的数交错数组可以有,而事实上CLR高兴地让我跑以下code:

They are treated differently, however. Jagged arrays are just arrays of (effectiely) regular* objects, and as such, aren't regulated in the same way as multi-dimensional arrays. There is no theoretical limit to the number of "dimensions" a jagged array can have, and in fact the CLR happily let me run the following code:

Array arr = new int[0];
for (int i = 0; i < Int32.MaxValue - 1; i++) {
    var newArr = Array.CreateInstance(arr.GetType(), 1);
    newArr.SetValue(arr, 0);
    arr = newArr;
}

右,直到我的系统变得完全没有反应而坠毁。没有异常被抛出。

Right up until my system became completely unresponsive and crashed. No exception was thrown.

限制了循环1000次迭代,你会发现所得到的阵列的类型是:

Limiting the loop to 1000 iterations, you find that the type of the resulting array is:

System.Int32[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]...

在此外,虽然我找不到这个源,维数交错数组可以具有大概甚至没有由单个对象的内存限制的限制,因为每个阵列仅具有对对象的引用。例如,交错数组(最终)的Int32 使用,,比方说1000嵌套因子和每个阵​​列2的元素,将占用的存储器可忽略量(即在其自己的),但所有的阵列结合将占用一个指数量

In addition, although I can't find a source for this, the number of dimensions a jagged array can have probably isn't even limited by the single object memory limitation, because each array only has references to objects. For example, a jagged array of (eventually) Int32 with a nesting factor of, say, 1000 and 2 elements per array, would take up a negligible amount of memory (that is, on its own), but all the arrays combined would take up an exponential amount.

*随着语法糖一点点。编码交错数组的更语法正确的方法是: INT [] [] ARR =新INT [] [0]; ,因为 INT [] 的元素类型。

* With a little bit of syntax sugar. A more syntactically correct way of encoding jagged arrays would be: int[][] arr = new int[][0]; because int[] is the element type.

维数组可以拥有的数量似乎是实现定义的。该数字不出现在CLI规范的任何位置,彼得提供的code 下面的Ideone的单运行良好2.8,在那里它会出现上限为255

The number of dimensions an array can have appears to be implementation-defined. The number doesn't appear anywhere in the CLI specification, and the code provided by Peter below runs fine on Ideone's mono-2.8, where it appears the limit is 255.

这篇关于像[1] [1] [1] [1] ... [1]在C#最大数组维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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