technical problem with jagged arrays
-
Monday, May 07, 2012 5:52 PM
hello, i'm running the following code in c#
using System; class Program { static void Main() { int val = 10; int i; int j; int[][] list = new int[2][]; list[0] = new int[5] { 1, 3, 5, 7, 9 }; list[1] = new int[5] { 2, 4, 6, 8, 10}; for (i = 0; i <list[0].Length;++i) for (j = 0; j < list[1].Length; ++j) { list[i][j] = val; Console.WriteLine("{0}", val); } } }
and when i run the code in the debug mode, it runs for couple seconds then it shows me the following message
IndexOutOfRangeException was unhanded
and directly underneath the Indexoutofrange it show the message
Index was outside the bounds of the array.
and the it leads me to the following link
i've read it, but it wasn't quite clear for me so anyone here could please tell me how can i solve this problem
thanks in advance.
PS, i hope any solution won't demand any kind of change to the program structure because i'm ought to follow this structure, yet i will change if there's no other to way to over come the problem
All Replies
-
Monday, May 07, 2012 5:59 PM
Your outer loop is getting the length of the first array, which is 5, and the inner array is getting the length of the second array, which is also five.
What is at list[4][4]?
The answer is nothing, the first array is only of size 2.
The for loop to go through all elements will look like this:
//set every item in the 2D array for(int i = 0; i < list.Length; i++) { for(int j = 0; j < list[i].Length; j++) { list[i][j] = val; } } //print every item in the 2D array for(int i = 0; i < list.Length; i++) { for(int j = 0; j < list[i].Length; j++) { Console.WriteLine(list[i][j]); } }
I would also discourage you from declaring i and j for the whole method; it doesn't make sense for them to exist outside of a given for loop. Variables should always be declared at the smallest scope that they can be to fulfill their tasks.
- Proposed As Answer by Ahmed Ibrahim - MSFTMicrosoft Employee Monday, May 07, 2012 6:46 PM
- Edited by servy42Microsoft Community Contributor Tuesday, May 08, 2012 2:33 PM
- Marked As Answer by Alexander SunModerator Monday, May 14, 2012 9:02 AM
-
Monday, May 07, 2012 6:17 PM
first of all thanks for replaying i do appreciate it, second i want to be crystal clear i declared a jagged array correctly right?
then i declared an integer value called val that has the value of 10 or any integer number what i want to do is to assign the values (all of them) to val, in simpler words i want all the elements of my jagged array to have the same value as"val"
thanks again :)
-
Monday, May 07, 2012 6:29 PM
"first of all thanks for replaying i do appreciate it, second i want to be crystal clear i declared a jagged array correctly right? "
It's a bit unusual, but it's not wrong, no. It does create the jagged array in the way that I'm pretty sure you meant to create it.
"then i declared an integer value called val that has the value of 10 or any integer number what i want to do is to assign the values (all of them) to val, in simpler words i want all the elements of my jagged array to have the same value as"val""
Yes, that's rather clear from your code, and that is also what my code does.
-
Monday, May 07, 2012 7:22 PM
You declared your jagged arrays just fine.
The problem with the for loop is that the condition for the outer loop is based off of the length of the first array in the second dimension, and what you want is the length of the first dimension. You should use the GetLength method and pass in 0 for the length of the first dimension.
-
Tuesday, May 08, 2012 7:29 AMModerator
Hi Servy42,
All things is nice. As a reminder, I think it is “j++” in the inner for loop statement.
Have a nice day.
Best Regards,
Alexander Sun [MSFT]
MSDN Community Support | Feedback to us
-
Sunday, May 13, 2012 8:31 PM
You declared your jagged arrays just fine.
The problem with the for loop is that the condition for the outer loop is based off of the length of the first array in the second dimension, and what you want is the length of the first dimension. You should use the GetLength method and pass in 0 for the length of the first dimension.
It's not a multi-dimensional array. It has no second dimension. No need to call GetLength(0). Just read the Length property. -
Sunday, May 13, 2012 9:26 PMLuis.fr, you're correct, my mistake.

