What is the maximum size of an array while using Array.sort() method?
in my application I have an array with 5000 elements. I have to sort these elements.But I am getting error of "Array index Out Of Bound Exception". Can anybody t开发者_运维问答ell me what can be the maximum size for the array to sort?
Should I use 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
. 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? 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.
The exception's .StackTrace
should reveal everything, of course.
And no: you shouldn't use ArrayList
here. Or pretty much anywhere else.
精彩评论