Array
-
1 is an array of arrays of arrays of ints, initialized to a length of 3 (that is, the outer array) 2 is an two-dimensional array of arrays of arrays of ints, initialized to a size of 3 by 2 (the outer, 2d array) They are already initialized, so maybe you meant to ask how to initialize the sub-arrays? For 1, do
= new int[length][];For 2, dosecondarray[x,y] = new int[length][];
-
oh Thanks for your answer it was very helpful,I want to know how can I fill them? for example int[][] test= new int[3][]; test[0] = new int[5] { 1, 3, 5, 7, 9 }; .... but I dont know about these arrays can you help me? Thanks again
-
oh Thanks for your answer it was very helpful,I want to know how can I fill them? for example int[][] test= new int[3][]; test[0] = new int[5] { 1, 3, 5, 7, 9 }; .... but I dont know about these arrays can you help me? Thanks again
What David means is: Every time you create an array, you have to initialize it:
int\[\] test = new int\[3\];
Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.
int\[\]\[\] test = new int\[3\]\[\];
Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:
int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, new int\[\] { 4, 5, 6 }, new int\[\] { 7, 8, 9, 10 } };
Or:
int\[\]\[\] test = new int\[3\]\[\]; for (int i = 0; i < test.Length; i++) { test\[i\] = new int\[3\]; }
You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:
int\[,\] test = new int\[3,4\];
This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:
int\[,\]\[\] test = new int\[3,4\]\[\];
Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a
foreach
loop rather than afor
loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:int\[\]\[\] test = new int\[3\]\[\]; foreach (int\[\] inner in test) { inner = new int\[3\]; }
Will give you a compilation error.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
What David means is: Every time you create an array, you have to initialize it:
int\[\] test = new int\[3\];
Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.
int\[\]\[\] test = new int\[3\]\[\];
Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:
int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, new int\[\] { 4, 5, 6 }, new int\[\] { 7, 8, 9, 10 } };
Or:
int\[\]\[\] test = new int\[3\]\[\]; for (int i = 0; i < test.Length; i++) { test\[i\] = new int\[3\]; }
You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:
int\[,\] test = new int\[3,4\];
This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:
int\[,\]\[\] test = new int\[3,4\]\[\];
Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a
foreach
loop rather than afor
loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:int\[\]\[\] test = new int\[3\]\[\]; foreach (int\[\] inner in test) { inner = new int\[3\]; }
Will give you a compilation error.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
Good one.
-
1 is an array of arrays of arrays of ints, initialized to a length of 3 (that is, the outer array) 2 is an two-dimensional array of arrays of arrays of ints, initialized to a size of 3 by 2 (the outer, 2d array) They are already initialized, so maybe you meant to ask how to initialize the sub-arrays? For 1, do
= new int[length][];For 2, dosecondarray[x,y] = new int[length][];
-
What David means is: Every time you create an array, you have to initialize it:
int\[\] test = new int\[3\];
Declares a variable that refers to an array of integers, calls it test, and assigns an array of three integers to it. You can now use test[0], test[1], and test[2] perfectly happily.
int\[\]\[\] test = new int\[3\]\[\];
Declares a variable that refers to an array of arrays of integers, calls it test, and assigns an array of three integer arrays to it. It does not assign the arrays of integers. To do that, you either need a static declaration, or a loop:
int\[\]\[\] test = new int\[3\]\[\] { new int\[\] { 1, 2, 3 }, new int\[\] { 4, 5, 6 }, new int\[\] { 7, 8, 9, 10 } };
Or:
int\[\]\[\] test = new int\[3\]\[\]; for (int i = 0; i < test.Length; i++) { test\[i\] = new int\[3\]; }
You need to repeat the process for each layer of array of arrays you add! Beware: arrays of arrays start to take significant amounts of space, very, very quickly... These are all "jagged" arrays: the inner arrays are not necessarily the same size. You can also declare rectangular arrays:
int\[,\] test = new int\[3,4\];
This declares a rectangular array of 12 elements in total, and assigns them all. You can happily use test[0,0], test[0,1]... etc. You can combine them as you did in your example:
int\[,\]\[\] test = new int\[3,4\]\[\];
Declares a rectangular array of 12 jagged arrays. You have to initialize each of these to a new array of ints before you can use them, either statically, or in a loop as before. Note: Normally, I would recommend using a
foreach
loop rather than afor
loop, but that is not possible when filling in jagged arrays, as you cannot change the loop variable:int\[\]\[\] test = new int\[3\]\[\]; foreach (int\[\] inner in test) { inner = new int\[3\]; }
Will give you a compilation error.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
your answer was very helpful for me thank you. :-D but may I ask a question? what is it? and how can I fill it? double[ , , ,] m_array4 = new double[,,,]; Thanks
-
your answer was very helpful for me thank you. :-D but may I ask a question? what is it? and how can I fill it? double[ , , ,] m_array4 = new double[,,,]; Thanks
Not like that! :laugh: If you look back at my previous answer, that is a rectangular array (doubles or ints, bools or TextBoxes, it doesn't matter what the type is) so you have to tell the compiler how big it is!
double[ , , ,] m_array4 = new double[,,,];
Will give a compilation error, because
new double[,,,]
does not specify all (or indeed any) of the dimensions. Without them, the space cannot be allocated, and it will complain.double[ , , ,] m_array4 = new double[3,4,5,6];
Will do it, but - remember I said they get big quickly? - it allocates space for 360 doubles...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together. Manfred R. Bihy: "Looks as if OP is learning resistant."
-
Hi everyone could you tell me what are these? and how we can initialize them? (1) int[][][] array = new int[3][][]; (2) int[,][][] secondarray= new int[3,2][][]; Thanks
-
Hi everyone could you tell me what are these? and how we can initialize them? (1) int[][][] array = new int[3][][]; (2) int[,][][] secondarray= new int[3,2][][]; Thanks
A jagged array is an array whose elements are arrays
http://msdn.microsoft.com/en-us/library/2s05feca.aspx[^]-ambarish-