Thread: how to init 2d array ?

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    how to init 2d array ?

    hi all~

    i'm new to C# and get confused this moment with 2d array initialization. i declared 2d array like this:
    Code:
    int[,] = new int[2, 1];
    and how to initialize them with the same line ?
    Never end on learning~

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by nvoigt
    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}}; 

    thanx but i tried it and it didnt work ???
    Never end on learning~

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    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] );
    	}
    }
    My Website

    "Circular logic is good because it is."

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    ok i succeed in establishing the 2d array but failed when output it ??? here is my code(an .aspx file):
    Code:
    <%
    	int[,] arr = new int[2, 3] {{1, 2, 3}, {4, 5, 6}};
    	Response.Write(arr[0][0]);
    %>
    the compiler hint the error happened at the line which i Response.Write the result, what's wrong there ???
    Never end on learning~

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by black
    ok i succeed in establishing the 2d array but failed when output it ??? here is my code(an .aspx file):
    Code:
    <%
    	int[,] arr = new int[2, 3] {{1, 2, 3}, {4, 5, 6}};
    	Response.Write(arr[0][0]);
    %>
    the compiler hint the error happened at the line which i Response.Write the result, what's wrong there ???
    to my surprise i found this would work:
    Code:
    Response.Write(arr[0, 0]);
    cant we get value with the format like arr[0][0] any more ???
    Never end on learning~

  8. #8
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by nvoigt
    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.
    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...
    Never end on learning~

  10. #10
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  11. #11
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    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~

  12. #12
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  13. #13
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Quote Originally Posted by nvoigt
    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
    thanx nvoigt i think i would learn this new feature to make performance better
    Never end on learning~

  14. #14
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    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]

  15. #15
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM