jagged array problems, who can help me?
Hi everybody,
I have problems here in using jagged array. Consider the following statements:
//line 1 int[][] jaggedArray;
//line 2 jaggedArray = new int[3][];
//line 3 jaggedArray[0] = new int[2] {1, 2};
//line 4 jaggedArray[1] = new int[3] {3, 4, 5};
//line 5 jaggedArray[2] = new int[4] {6, 7, 8, 9};
//line 6 foreach(int[] array in jaggedArray)
//line 7 {
//line 8 foreach(int subArray in array)
//line 9 {
//line 10
Console.WriteLine(subArray);
//line 11 }
//line 12 }
Questions:
1. At line 2, what does "3" means? Is it means that the jagged array is containing 3 rows of sub-arrays?
2. From Wrox C# book, I came to know that to loop through the jagged array, we have to use nested "foreach" loops as above.The output of the statements above will generate integer numbers from 1 to 9 printed on the output screen.
How if I want to print only the output from either "jaggedArray[0]" or "jaggedArray[1]" or "jaggedArray[2]", or any possible combinations not more than two sub arrays, so is there any way to do it? For example, i might want to generate integers from 1-2 on the screen, not 1-9.
3. Beside using "foreach" loops to loop through jaggedArray, is "for" loops capable of generating the same result? If yes, how? If no, then is there any alternative way for doing that?
I really appreciate that if anyone can explain to me clearly on this. Thank you.
|