Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I have taken a jagged array of size 250, and another array varies, i.e. jaggedArray[250][], so I need to find the maximum array of the total 250 arrays. How is that possible?

Thanks in advance
Posted
Updated 29-Sep-10 0:02am
v2

But if i want the index for that array , how can do that in LINQ :sigh:
 
Share this answer
 
Incase of writing for loop you can use Linq to do that.

C#
int maxLength = (from m in jaggedarray
                            select m.Max()).Max();
 
Share this answer
 
C#
int[][] jaggedarray = new int[3][];
            jaggedarray[0] = new int[] { 1, 2, 3 };
            jaggedarray[1] = new int[] { 1, 2,3,4,5};
            jaggedarray[2] = new int[] { 1,};
            int max = 0;
            for (int i = 0; i < jaggedarray.Length; i++)
            {
                if (jaggedarray[i].Length > max)
                    max = jaggedarray[i].Length;
            }


where max is the solution you want.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
Sauro Viti 29-Sep-10 8:32am    
Just a little improvement... As the arrays could be not initialized, to avoid exceptions change the if statement inside the for loop as follow:

if (jaggedarray[i] != null && jaggedarray[i].Length > max)
max = jaggedarray[i].Length;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900