hi all~
i'm new to C# and get confused this moment with 2d array initialization. i declared 2d array like this:
and how to initialize them with the same line ?Code:int[,] = new int[2, 1];
hi all~
i'm new to C# and get confused this moment with 2d array initialization. i declared 2d array like this:
and how to initialize them with the same line ?Code:int[,] = new int[2, 1];
Never end on learning~
If you need to do so, you can initialize n-dimensional arrays like this:
PHP Code:
int[,] array = new int[2,3] {{1,2,3},{5,6,7}};
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
Originally Posted by nvoigt
thanx but i tried it and it didnt work ???
Never end on learning~
it should work. if it does not, then something else is wrong with your program.
here is a quick little program using that same code that nv posted:
Code:using System; class myprog { static void Main ( string [] args ) { int[,] array = new int[2,3] {{1,2,3},{5,6,7}}; for ( int k = 0; k < 2; k++ ) for ( int j = 0; j < 3; j++ ) Console.WriteLine ( array[k,j] ); } }
ok i succeed in establishing the 2d array but failed when output it ??? here is my code(an .aspx file):
the compiler hint the error happened at the line which i Response.Write the result, what's wrong there ???Code:<% int[,] arr = new int[2, 3] {{1, 2, 3}, {4, 5, 6}}; Response.Write(arr[0][0]); %>
Never end on learning~
So what kind of error is it ? Can you post the exact error message please ?
If it's a runtime error, can you catch the exception and output the stack trace and error message please ?
( Both are properties of the exception object )
Edit:
Also, make sure you don't mix jagged and rectangular arrays.
array[][] is not the same as array[,]
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
to my surprise i found this would work:Originally Posted by black
cant we get value with the format like arr[0][0] any more ???Code:Response.Write(arr[0, 0]);
Never end on learning~
I see, you found it while I was editing my post
int [,] is a rectangular array, that is an array that has n dimensions.
int[][] is a jagged array, that is an array of arrays.
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
thanx but... what's the difference between them ? i remembered C++ and C also index multi dimention array value with something like arr[1][2][3], really confused...Originally Posted by nvoigt
Never end on learning~
In a rectangular array, each column and row has to have the same size like this: ( array[4,7] )
0123456
0123456
0123456
0123456
while a jagged array is an array of arrays, where you have two different dimensions and as such you can put another array length into each line: ( array[4][] )
0123456789
01234
01234567
0123456
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
thanx nvoigt, and i read the part for jagged arraay from C# primer, which has the same remarks
but i'd really used to deal with multi arrays like this arr[1][2], can rectangle array apply this way or is there any substitute for arr[1, 2] ?
Never end on learning~
Well, if you do some initialization work yourself and take a slight performance loss into account ( no biggie with a garbage collected language anyway ) you can build your 2d aray as a jagged array of same size arrays and use the [][] notation anyway, but it's confusing for readers and a lot of extra work just to avoid the comma
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
thanx nvoigt i think i would learn this new feature to make performance betterOriginally Posted by nvoigt
Never end on learning~
salut je ne parle pas l'anglai mais tupeux traduire cette page
bon en ce qui concerne l' init du 2d
voila comment tu peux proceder
/*1er*/
int t[variablename][variablename];/*matrice */
ou
int t[?1][?2]={les valeur pour ?1 seperer d'une vergile }{les valeur de ?2 separer d'une vergule}
je croit bien que tu a comprit
si ce n'ai pas le cas tu peux m'expliquer ton problem exact et je peux t'offrir de laide
mon e-mail est :[email protected]
Sorry, I don't speak french. Maybe you could rephrase your question in english ( or german so I can translate ) ?
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.