简体   繁体   English

使用Array.sort()方法时数组的最大大小是多少?

[英]What is the maximum size of an array while using Array.sort() method?

in my application I have an array with 5000 elements. 在我的应用程序中,我有一个包含5000个元素的数组。 I have to sort these elements.But I am getting error of "Array index Out Of Bound Exception". 我必须对这些元素进行排序。但是我收到“数组索引超出界限异常”的错误。 Can anybody tell me what can be the maximum size for the array to sort? 谁能告诉我要排序的数组的最大大小是多少?

Should I use ArrayList ?? 我应该使用ArrayList吗?

There is no specific limit - you are only constrained by memory here, and at this point the array already exists, so this isn't a limitation of Array.Sort . 没有特定的限制-您在这里仅受内存限制,此时数组已经存在,因此这不是Array.Sort的限制。 For example: 例如:

int[] arr = new int[500000];
Random rand = new Random();
for (int i = 0; i < arr.Length; i++) arr[i] = rand.Next();
Array.Sort(arr); // works just fine

I suspect you might (for example) have an IComparable[<T>] implementation that is throwing an error internally? 我怀疑您可能(例如)有一个IComparable[<T>]实现在内部抛出错误? Or alternatively, perhaps this error has nothing at all to do with Array.Sort , and you have simply considered the wrong line as the cause. 或者,也许此错误与Array.Sort 完全 Array.Sort ,而您只是将错误的行视为原因。

The exception's .StackTrace should reveal everything, of course. 当然,异常的.StackTrace应该显示所有内容。

And no: you shouldn't use ArrayList here. 否:您不应该在这里使用ArrayList Or pretty much anywhere else. 或几乎其他任何地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM